websocket

package module
v0.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: MIT Imports: 6 Imported by: 0

README

WebSocket

A WebSocket Server with Subprotocol support.

Features

Socket handling

We use goroutines + channels to read from and write to a WebSocket. PING/PONG messages are also handled internally.

Subprotocol registry

An API is provided to create subprotocols and register messages to them.

Message encoding

Message names (topics) are strings with a maximum length of 225 bytes, and the payload is a byte array. You can use any encoder/decoder you like as long as you can decode/encode it at the client.

Socket management

Socket operations are handled internally with sane defaults. There is an optional ClientID function that will run on each new connection, allowing you to group sockets by user ID or any other identifier.

Rooms

You can further group clients into rooms

TODO

  • Unit tests
  • JS-Client implementation
  • Client implementation
  • More configurability
  • Default subprotocol
  • Logging interface instead of logrus.Entry
  • More comments
  • JSONSubprotocol: a subprotocol that works with interface{} instead of []byte and encodes to/decodes from JSON

Credit

This library is a wrapper around the gorilla/websocket implementation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMessageTooLong = errors.New("websocket: message too long")
)

Functions

This section is empty.

Types

type MessageHandler

type MessageHandler func(*Socket, []byte)

type Socket

type Socket struct {
	// contains filtered or unexported fields
}

func NewSocket

func NewSocket(conn *websocket.Conn, subp *Subprotocol, log *logrus.Entry) *Socket

func (*Socket) Close

func (s *Socket) Close() error

func (*Socket) Send

func (s *Socket) Send(topic string, payload []byte) error

Send is a helper method to send a message to current socket

func (*Socket) SendToClient

func (s *Socket) SendToClient(topic string, payload []byte) error

SendToClient is a helper method to send a message to all sockets of the same client

type Subprotocol

type Subprotocol struct {
	FallbackHandler func(socket *Socket, message string, payload []byte)
	ClientID        func(r *http.Request) (clientID string)
	// contains filtered or unexported fields
}

func (*Subprotocol) Broadcast

func (s *Subprotocol) Broadcast(topic string, payload []byte) error

func (*Subprotocol) Handle

func (s *Subprotocol) Handle(topic string, handler MessageHandler)

Handle defines the handler for a message. Handlers are not called concurrently and will block further messages from being handled on the same Socket to ensure message ordering.

func (*Subprotocol) JoinRoom

func (s *Subprotocol) JoinRoom(name string, socket *Socket)

func (*Subprotocol) LeaveRoom

func (s *Subprotocol) LeaveRoom(name string, socket *Socket)

func (*Subprotocol) SendToClient

func (s *Subprotocol) SendToClient(id string, topic string, payload []byte) error

func (*Subprotocol) SendToRoom

func (s *Subprotocol) SendToRoom(name string, topic string, payload []byte) error

func (*Subprotocol) SendToSocket

func (s *Subprotocol) SendToSocket(socket *Socket, topic string, payload []byte) error

type WebSocketServer

type WebSocketServer struct {
	// contains filtered or unexported fields
}

func NewWebSocketServer

func NewWebSocketServer() *WebSocketServer

func (*WebSocketServer) ServeHTTP

func (ws *WebSocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface

func (*WebSocketServer) SetLogger

func (ws *WebSocketServer) SetLogger(logger *logrus.Entry)

func (*WebSocketServer) Subprotocol

func (ws *WebSocketServer) Subprotocol(name string) (subp *Subprotocol)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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