websocket

package module
v0.0.0-...-3a433ef Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 11 Imported by: 0

README

websocket

I wanted a more concise WebSocket interface than what Gorilla provides out of the box, so I wrote this package.

Usage

The main entry point for this package is the ServeWS function, which returns an http.HandlerFunc that will upgrade client connections and subsequently handle reading from and writing to the client. The caller simply needs to supply zero or more callback functions that are invoked on every message received from the client.

Interfaces

There are two interfaces: Client and Manager. The package provides example implementations, but consumers are free to implement their own.

Client functions as a middleman between your service and client websocket connection. It is responsible for reading (writing) messages from (to) the client and invoking the callbacks on the client messages, which are assumed to be of type []byte.

Manager is a convenience interface for managing N client connections; the example implementation simply stores them in a map. There's also a Broadcaster type that implements the Manager interface and allows you to broadcast a message to all clients.

Usage

See the tests for example usage.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultSetupConn

func DefaultSetupConn(c *websocket.Conn)

DefaultSetupClient is an example implementation of a function that sets up a websocket connection.

func DefaultUpgrader

func DefaultUpgrader(origins []string) websocket.Upgrader

func ServeWS

func ServeWS(

	upgrader websocket.Upgrader,

	connSetup func(*websocket.Conn),

	clientFactory func(*websocket.Conn) Client,

	onCreate func(context.Context, context.CancelFunc, Client),

	onDestroy func(Client),

	ping time.Duration,

	msgHandlers []MessageHandler,
) http.HandlerFunc

ServeWS upgrades HTTP connections to WebSocket, creates the Client, calls the onCreate callback, and starts goroutines that handle reading (writing) from (to) the client.

Types

type Broadcaster

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

Broadcaster is an example implementation of Manager that has a Broadcast method that writes the supplied message to all clients.

func (*Broadcaster) Broadcast

func (bb *Broadcaster) Broadcast(b []byte)

func (Broadcaster) Clients

func (m Broadcaster) Clients() []Client

Clients returns the currently managed Clients.

func (Broadcaster) RegisterClient

func (m Broadcaster) RegisterClient(ctx context.Context, cf context.CancelFunc, c Client)

RegisterClient adds the Client to the Manager's store.

func (Broadcaster) Run

func (m Broadcaster) Run(ctx context.Context)

Run runs in its own goroutine processing (un)registration requests.

func (Broadcaster) UnregisterClient

func (m Broadcaster) UnregisterClient(c Client)

UnregisterClient removes the Client from the Manager's store.

type Client

type Client interface {
	io.Writer
	io.Closer

	// WriteForever is responsible for writing messages to the client (including
	// the regularly spaced ping messages)
	WriteForever(context.Context, func(Client), time.Duration)

	// ReadForever is responsible for reading messages from the client, and passing
	// them to the message handlers
	ReadForever(context.Context, func(Client), ...MessageHandler)

	// SetLogger allows consumers to inject their own logging dependencies
	SetLogger(any) error

	// Log allows implementors to use their own logging dependencies
	Log(int, string, ...any)

	// Wait blocks until the client is done processing messages
	Wait()
}

Client is an interface for reading from and writing to a websocket connection. It is designed to be used as a middleman between a service and a client websocket connection.

func NewClient

func NewClient(c *websocket.Conn) Client

NewClient returns a new Client from a *websocket.Conn. This can be passed to ServeWS as the client factory arg.

type Manager

type Manager interface {
	Clients() []Client
	RegisterClient(context.Context, context.CancelFunc, Client)
	UnregisterClient(Client)
	Run(context.Context)
}

Manager maintains a set of Clients.

func NewBroadcaster

func NewBroadcaster() Manager

func NewManager

func NewManager() Manager

type MessageHandler

type MessageHandler func(Client, []byte)

Jump to

Keyboard shortcuts

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