sockets

package
v0.0.0-...-5525a2d Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2019 License: MIT Imports: 8 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// ErrServer tells the client there was a server error
	ErrServer wsError = "ERR_SERVER"
	// ErrBadMessage tells the client that the message they sent isn't supported
	ErrBadMessage wsError = "ERR_BADMESSAGE"
	// ErrUnauthorized tells the client they don't have permissions to access the requested topic
	ErrUnauthorized wsError = "ERR_UNAUTHORIZED"
	// ErrInvalidTopic tells the client the topic they sent isn't valid
	ErrInvalidTopic wsError = "ERR_INVALIDTOPIC"
)
View Source
const (
	// TypeResponse is used to tell the client they have a response
	// for an action such as listening or unlistening to topics
	TypeResponse wsType = "RESPONSE"
	// TypeMessage tells the client they have a new payload from the server
	TypeMessage wsType = "MESSAGE"
	// TypeListen tells the Server that you want to listen to a set topic(s)
	TypeListen wsType = "LISTEN"
	// TypeUnlisten tells the Server that you want to unlisten to a set topic(s)
	TypeUnlisten wsType = "UNLISTEN"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Send chan *MessagePayload

	User *security.User
	// contains filtered or unexported fields
}

Client is a connection to the Instance

func (*Client) AddTopics

func (c *Client) AddTopics(topic []string)

AddTopics adds a topic to a client

func (*Client) GetTopics

func (c *Client) GetTopics() [][]string

GetTopics returns a clients active topics

func (*Client) RemoveTopics

func (c *Client) RemoveTopics(removeTopic []string)

RemoveTopics removes a topic from a client

type Instance

type Instance struct {
	Clients    map[*Client]bool
	Inbound    chan []byte
	Register   chan *Client
	Unregister chan *Client

	Store *redis.Client
	// contains filtered or unexported fields
}

Instance is a pubsub server instance

func New

func New(store *redis.Client) *Instance

New creates a new pubsub instance

func (*Instance) AddHandler

func (i *Instance) AddHandler(q func(*Instance, *Client, []string, MessagePayload), topic string)

AddHandler creates a response function for a given topic

func (*Instance) Handler

func (i *Instance) Handler(w http.ResponseWriter, r *http.Request)

Handler is the websocket upgrade handler

func (*Instance) Publish

func (i *Instance) Publish(topics []string, data *MessagePayload)

Publish publishes a message to all the connected clients listening to a set topic(s)

func (*Instance) Start

func (i *Instance) Start()

Start listens for new websocket connections

type MessagePayload

type MessagePayload struct {
	Type  wsType             `json:"type"`
	Nonce string             `json:"nonce,omitempty"`
	Error wsError            `json:"error,omitempty"`
	Data  MessagePayloadData `json:"data,omitempty"`
}

MessagePayload is the message from clients to the server and server to clients

type MessagePayloadData

type MessagePayloadData struct {
	Topics []string    `json:"topics,omitempty"`
	Data   interface{} `json:"data,omitempty"`
}

MessagePayloadData is the main data object of the message payload

Jump to

Keyboard shortcuts

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