Documentation
¶
Index ¶
- Variables
- type Client
- type Connection
- func (wc *Connection) Close() error
- func (wc *Connection) CloseCtx() context.Context
- func (wc *Connection) CloseWithCause(cause error)
- func (wc *Connection) Err() error
- func (wc *Connection) Read() (messageType MessageType, p []byte, err error)
- func (wc *Connection) Write(messageType MessageType, data []byte) error
- type ConnectionMetadata
- type MessageType
- type Messenger
- type OnConnectFn
- type OnDisconnectFn
- type Server
- type ServerOpt
Constants ¶
This section is empty.
Variables ¶
var ErrNotConnected = errors.New("not connected")
var ErrNotReconnecting = errors.New("not reconnecting")
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client lazily connects to the configured endpoint on writes/reads when necessary, until the reconnecting-client is Close-ed. The status of the current connection can be checked with Err().
func (*Client) Err ¶
Err returns nil if the client is connected. It returns ErrNotConnected if not connected. It returns context.Canceled if the Reconnecting client was closed. It returns another error if the underlying connection failed or closed in some way. Client will attempt re-connection upon next Read or Write.
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection is an opinionated wrapper around the Gorilla websocket connection library. It handles pings/pongs/reads/writes/close. This manages the closing of a connection by reporting *why* the connection was closed, and sending a close-message to the wbesocket if we are closing.
func (*Connection) Close ¶
func (wc *Connection) Close() error
Close closes the connection, if it's not already closed. It then returns the error of the connection closing, or nil if successfully closed without issue.
func (*Connection) CloseCtx ¶
func (wc *Connection) CloseCtx() context.Context
CloseCtx returns the context that terminates when the connection closed. The context Cause shares the reason for closure. This may simply be "context.Canceled" if Close() was called. This may be a websocket.CloseError if the connection itself was broken.
func (*Connection) CloseWithCause ¶
func (wc *Connection) CloseWithCause(cause error)
func (*Connection) Err ¶
func (wc *Connection) Err() error
Err is a shorthand for the Cause error of the CloseCtx.
func (*Connection) Read ¶
func (wc *Connection) Read() (messageType MessageType, p []byte, err error)
Read reads from the connection. Note: reads on the underlying connection are timed out by the underlying ping-pong message system.
func (*Connection) Write ¶
func (wc *Connection) Write(messageType MessageType, data []byte) error
Write writes to the connection.
type ConnectionMetadata ¶
type MessageType ¶
type MessageType uint
MessageType is a type-safe enum, replacing the message-types by the underlying gorilla-websocket library.
const ( // TextMessage denotes a text data message. The text message payload is // interpreted as UTF-8 encoded text data. TextMessage MessageType = 1 // BinaryMessage denotes a binary data message. BinaryMessage MessageType = 2 // CloseMessage denotes a close control message. The optional message // payload contains a numeric code and text. Use the FormatCloseMessage // function to format a close message payload. CloseMessage MessageType = 8 // PingMessage denotes a ping control message. The optional message payload // is UTF-8 encoded text. PingMessage MessageType = 9 // PongMessage denotes a pong control message. The optional message payload // is UTF-8 encoded text. PongMessage MessageType = 10 )
The message types are defined in RFC 6455, section 11.8.
func (MessageType) String ¶
func (typ MessageType) String() string
type Messenger ¶
type Messenger interface { Write(messageType MessageType, data []byte) error Read() (messageType MessageType, p []byte, err error) Close() error Err() error }
type OnConnectFn ¶
type OnConnectFn[E any] func(c *Connection, meta *ConnectionMetadata) (E, error)
type OnDisconnectFn ¶
type OnDisconnectFn[E any] func(e E)
type ServerOpt ¶
type ServerOpt[E any] func(c *serverConfig[E])
func WithOnDisconnect ¶
func WithOnDisconnect[E any](onDisconnect OnDisconnectFn[E]) ServerOpt[E]