client

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: 13 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Client contains a socket client which connects to one or many servers and channels on those servers.

func New

func New(opts ...OptFunc) *Client

New will setup a new websocket client which will connect to the server at the provided uri.

func (*Client) Close

func (c *Client) Close()

Close will ensure the client is gracefully shut down.

func (*Client) HasChannel

func (c *Client) HasChannel(channelID string) bool

HasChannel will check to see if a client is connected to a channel.

func (*Client) JoinChannel

func (c *Client) JoinChannel(host, channelID string, headers http.Header, params map[string]string) error

JoinChannel will connect the client to the supplied host and channelID, returning an error if it cannot connect.

If you need to authenticate with the server or send meta, add header/s.

func (*Client) LeaveChannel

func (c *Client) LeaveChannel(channelID string, headers http.Header)

LeaveChannel will disconnect a client from a channel.

func (*Client) Publish

func (c *Client) Publish(req sockets.Request) error

Publish will broadcast a message to the server and wait for an error.

func (*Client) RegisterListener

func (c *Client) RegisterListener(msgType string, fn sockets.HandlerFunc)

RegisterListener will add a new listener to the client.

func (*Client) WithChannelClosedListener

func (c *Client) WithChannelClosedListener(l sockets.HandlerFunc) *Client

WithChannelClosedListener will replace the default channel closed handler which simply prints a debug message. This is raised when the server explicitly closes a channel.

By adding your own you can handle the channel close in a custom manner.

func (*Client) WithChannelExpiredListener

func (c *Client) WithChannelExpiredListener(l sockets.HandlerFunc) *Client

WithChannelExpiredListener will replace the default channel expired handler which simply prints a debug message.

By adding your own you can handle the channel expiry in a custom manner.

func (*Client) WithErrorHandler

func (c *Client) WithErrorHandler(e sockets.ClientErrorHandlerFunc) *Client

WithErrorHandler allows a user to overwrite the default error handler.

func (*Client) WithJoinRoomFailedListener

func (c *Client) WithJoinRoomFailedListener(l sockets.HandlerFunc) *Client

WithJoinRoomFailedListener will replace the default room join failed handler with a custom one. This will allow the implementor to define their own logic after a room is joined.

func (*Client) WithJoinRoomSuccessListener

func (c *Client) WithJoinRoomSuccessListener(l sockets.HandlerFunc) *Client

WithJoinRoomSuccessListener will replace the default room join success handler with a custom one. This will allow the implementor to define their own logic after a room is joined.

func (*Client) WithMiddleware

func (c *Client) WithMiddleware(mws ...sockets.MiddlewareFunc) *Client

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.

func (*Client) WithServerErrorHandler

func (c *Client) WithServerErrorHandler(e sockets.ClientErrorMsgHandlerFunc) *Client

WithServerErrorHandler allows a user to overwrite the default server error handler.

This handles ErrorMessage responses from a server in response to a client send.

type OptFunc

type OptFunc func(c *opts)

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

func WithInfiniteReconnect

func WithInfiniteReconnect() OptFunc

WithInfiniteReconnect will make the client listen forever for the server to reconnect in the event of a connection loss.

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 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 WithReconnect

func WithReconnect() OptFunc

WithReconnect will enable reconnects from a client, in the event of a connection loss with a server the client will attempt to reconnect.

Default values are to retry 3 times with a 30 second wait between retry.

func WithReconnectAttempts

func WithReconnectAttempts(attempts int) OptFunc

WithReconnectAttempts will overwrite the default connection attempts of 3 with value attempts, when this value is exceeded the connection will cease to re-connect and exit.

func WithReconnectTimeout

func WithReconnectTimeout(t time.Duration) OptFunc

WithReconnectTimeout will overwrite the default timeout between reconnect attempts of 30 seconds with value t.

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.

Jump to

Keyboard shortcuts

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