server

package
v0.0.12-beta Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OptFunc

type OptFunc func(c *opts)

OptFunc defines a functional option to pass to the server at setup time.

func WithChannelTimeout

func WithChannelTimeout(t time.Duration) OptFunc

WithChannelTimeout will setup the server with a timeout that is set on each channel as it is created.

Default is to NOT expire, adding this will cause the server to automatically close the channels and emit a channel.expired message to each client.

func WithMaxMessageSize

func WithMaxMessageSize(s int64) OptFunc

WithMaxMessageSize defines the maximum message size in bytes that the client will accept. Default is 512 bytes.

func WithNoChannelTimeout

func WithNoChannelTimeout() OptFunc

WithNoChannelTimeout will setup the server so it won't ever expire channels, leaving it up to clients to disconnect.

This is the default option.

func WithPingPeriod

func WithPingPeriod(i time.Duration) OptFunc

WithPingPeriod will define the break between pings to the server. This should always be less than PongTimeout.

func WithPongTimeout

func WithPongTimeout(t time.Duration) OptFunc

WithPongTimeout defines the wait time the client will wait for a pong response from the server. Default is 60 seconds.

func WithWriteTimeout

func WithWriteTimeout(t time.Duration) OptFunc

WithWriteTimeout defines the timeout length that the client will wait before failing the write. Default is 60 seconds.

type SocketServer

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

SocketServer is a central point that connects peers together. It manages connections and channels as well as sending of messages to peers.

It can have listeners setup both for channel broadcast and direct broadcast.

func New

func New(opts ...OptFunc) *SocketServer

New will setup and return a new instance of a SocketServer.

func (*SocketServer) Broadcast

func (s *SocketServer) Broadcast(channelID string, msg *sockets.Message)

Broadcast will send a message to a channel.

This is used if a server event happens that needs to be sent to all clients without a message being sent first via a listener.

func (*SocketServer) BroadcastAwait

func (s *SocketServer) BroadcastAwait(ctx context.Context, channelID string, msg *sockets.Message) (*sockets.Message, error)

BroadcastAwait will send a broadcast to a channel and wait for a response, this will simply act on the first response to hit the server, if multiple peers respond, only the first will be returned.

The function will return if a msg is returned OR an error is returned OR the ctx times out.

func (*SocketServer) BroadcastDirect

func (s *SocketServer) BroadcastDirect(clientID string, msg *sockets.Message)

BroadcastDirect will send a message directly to a client.

This is used if a server event happens that needs to be sent to a client without a message being sent first via a listener.

func (*SocketServer) Close

func (s *SocketServer) Close()

Close should always be called in a defer to allow the server to gracefully shutdown and close underling connections.

func (*SocketServer) CloseChannel

func (s *SocketServer) CloseChannel(channelID string)

CloseChannel will close a channel and close all connections within.

func (*SocketServer) HasChannel

func (s *SocketServer) HasChannel(channelID string) bool

HasChannel will check to see if the server has a channel connection established.

func (*SocketServer) Listen

func (s *SocketServer) Listen(conn *websocket.Conn, channelID string) error

Listen will start up a new listener for the received connection and channelID.

This would be called after an Upgrade call in an http handler usually in a go routine.

func (*SocketServer) OnChannelClose

func (s *SocketServer) OnChannelClose(fn func(channelID string))

OnChannelClose is called when all clients have left a channel and it is closed.

func (*SocketServer) OnChannelCreate

func (s *SocketServer) OnChannelCreate(fn func(channelID string))

OnChannelCreate is called when a new channel is created.

func (*SocketServer) OnClientJoin

func (s *SocketServer) OnClientJoin(fn func(clientID, channelID string))

OnClientJoin is called when a client joins a channel.

func (*SocketServer) OnClientLeave

func (s *SocketServer) OnClientLeave(fn func(clientID, channelID string))

OnClientLeave is called when a client leaves a channel.

func (*SocketServer) RegisterChannelHandler

func (s *SocketServer) RegisterChannelHandler(name string, fn sockets.HandlerFunc) *SocketServer

RegisterChannelHandler will add a handler that when sending a message will send to ALL clients connected to the channelID in the message.

func (*SocketServer) RegisterDirectHandler

func (s *SocketServer) RegisterDirectHandler(key string, fn sockets.HandlerFunc) *SocketServer

RegisterDirectHandler will register handlers that respond ONLY to the client that sent them a message, no other clients will receive the notification.

func (*SocketServer) WithErrorHandler

func (s *SocketServer) WithErrorHandler(fn sockets.ServerErrorHandlerFunc) *SocketServer

WithErrorHandler can be used to overwrite the default error handler.

func (*SocketServer) WithMiddleware

func (s *SocketServer) WithMiddleware(mws ...sockets.MiddlewareFunc) *SocketServer

WithMiddleware will append the middleware funcs to any already registered middleware functions. When adding middleware, it is recommended to always add a PanicHandler first as this will ensure your application has the best chance of recovering. There is a default panic handler available under sockets.PanicHandler.

Jump to

Keyboard shortcuts

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