Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config represents the configuration that is needed to create a new Connection.
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection wraps the websocket connection and add more functionalities to it. Each client that connected to the websocket server has a Connection.
func NewConnection ¶
func NewConnection( ctx context.Context, conn *websocket.Conn, helper helper, authFunc auth.AuthenticateFunc, logger log.Logger, options ...Option, ) *Connection
NewConnection creates a new instance of Connection that wraps the input websocket.Conn.
It runs read and write goroutines to read the client messages and write the server messages to the connection.
websocket.Conn can be close by calling the Connection.Close method. It will stop the goroutines and release the memory.
Cancelling input context, closes the connection. So, the input context must be the application context not the request context.
func (*Connection) Authenticate ¶
func (c *Connection) Authenticate() error
Authenticate validates the existing token and update the connection token if the token has been updated.
func (*Connection) AuthenticateAndStore ¶
func (c *Connection) AuthenticateAndStore(token string) error
AuthenticateAndStore validates the input token by calling the auth function that client already implemented. Stores the token details in the receiver if it is valid. Otherwise, returns err.
func (*Connection) Close ¶
func (c *Connection) Close() error
Close closes the websocket.Conn and cancel the connection context. Cancelling the context causes closing the running read and write goroutines. The closing connection process is singleton.
func (*Connection) SendMessage ¶
func (c *Connection) SendMessage(message []byte) error
SendMessage sends the input message to the outbound channel. Connection.write method will receive this message and writes it to the client.
Before sending the input message, it checks if the connection is still open or not. If it is closed, closes the outbound channel and return error.
Returns error if outbound buffer is full.
func (*Connection) UserID ¶
func (c *Connection) UserID() *string
UserID return the token userID if token is not nil and userID is not empty.
type Option ¶
type Option func(*Config)
func WithCollector ¶
func WithCollector(in collector) Option
func WithOutboundBufferSize ¶
func WithPingMessageFunc ¶
func WithPingMessageFunc(messageFunc PingMessageFunc) Option
func WithPingPeriod ¶
func WithPongWait ¶
type PingMessageFunc ¶
type PingMessageFunc func() []byte