websocket_client

package module
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2021 License: BSD-3-Clause Imports: 9 Imported by: 0

README

It's worth pointing out that this is a complete rewrite from the original forked go-socket.io-client which is tailored around my current requirement. But I believe it will definitely work with any base websocket server.


package main

import (
	"log"
	"net/http"
	"sync"
	"time"

	WS "github.com/Akumzy/websocket-client"
)

var token = "jwt-token"

type User struct {
	ID   string `json:"_id,omitempty"`
	Name string `json:"name,omitempty"`
}

var client *WS.Client

func main() {
    // Your custom headers
	header := http.Header{"Authorization": []string{token}}
    client = WS.NewClient("ws://localhost:4444/ws", WS.Options{Headers: header})
    // Subscribe to server sent events
	client.On("update", func(user User) {
		log.Println(user)
	})
	client.On("ready", func(ready bool) {
		log.Println(ready)
		 sendMessageWithAcknowledgment()
    })
    // Send message to your websocket server
    client.Send("feed-me-more", time.Now())

	err := client.Connect()
	log.Println(err)
}
func sendMessageWithAcknowledgment() {
	var wg sync.WaitGroup
    wg.Add(1)

	var mode bool
	reply, err := client.Send("bingo", true, true)
	if err != nil {
		panic(err)
    }
    // Make sure to set a timeout
	timer := time.AfterFunc(time.Second*20, func() {
		wg.Done()
	})
	client.On(reply, func(y bool) {
		timer.Stop()
		mode = y
		wg.Done()
	})
	wg.Wait()
	log.Println("Mode: ", mode)
}

Messages sent or received from server most have this object structure

type Payload struct {
	// Event name used to identify event handlers
	Event string `json:"event"`
	// Message payload
	Data interface{} `json:"data"`
	// Ack is string(event name) that will be sent to server which
	// an acknowledgment will be published/sent to and the client will
	// need to get the event name from client.Send method after emitting an
	// event to server. (optional)
	Ack string `json:"ack"`
}

This package depends on awesome gorilla/websocket

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Ready *bool
	// contains filtered or unexported fields
}

func NewClient

func NewClient(uri string, options Options) *Client

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) On

func (client *Client) On(message string, f interface{}) (err error)

On method is used to subscribe to server sent events

func (*Client) OnDisconnect added in v0.0.6

func (c *Client) OnDisconnect(handler func(err error, statusCode int))

func (*Client) RemoveHandler added in v0.0.2

func (client *Client) RemoveHandler(message string)

RemoveHandler

func (*Client) Send

func (client *Client) Send(event string, data ...interface{}) (string, error)

Send method sends message to your Websocket server It can take only take up to 3 arguments first being the event-name your server handler will subscribed to (required) second being the message payload of any type (optional) and third is being an indicator (recommended as bool) showing that this message requires an acknowledgement

type ID

type ID struct {
	sync.Mutex
	// contains filtered or unexported fields
}

type Options

type Options struct {
	Headers        http.Header
	AutoReconnect  bool
	ReconnectAfter time.Duration
}

type Payload

type Payload struct {
	// Event name used to identify event handlers
	Event string `json:"event,omitempty"`
	// Message payload
	Data interface{} `json:"data,omitempty"`
	// Ack is string(event name) that will be sent to server which
	// an acknowledgment will be published/sent to and the client will
	// need to get the event name from client.Send method after emitting an
	// event to server.
	Ack string `json:"ack,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL