Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultSetupConn ¶
DefaultSetupClient is an example implementation of a function that sets up a websocket connection.
func DefaultUpgrader ¶
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) 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.
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