websocket

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a client to a websocket server It is responsible for - sending and receiving messages to/from the server - handling ping/pong heartbeat to keep the connection alive

func NewClient

func NewClient(addr string, cfg *ClientConfig, decode func(io.Reader) (interface{}, error)) (*Client, error)

NewClient creates a new websocket client

  • wsConn is the underlying websocket connection
  • decode is the function that is used to decode incoming messages

func (*Client) Errors

func (c *Client) Errors() <-chan error

Errors notifies the users of any errors that occurs during the connection Users SHOULD listen to this channel to be notified of any errors that occurs indicating that the connection is no longer usable After receiving an error, the Client is no longer usable and the user MUST call Stop to ensure that the connection is properly closed and resources are released

func (*Client) Messages

func (c *Client) Messages() <-chan *IncomingMessage

Messages returns a channel that receives incoming messages

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, msgType int, encode func(io.Writer) error) error

sendMessage schedules a message to be sent to the server sendMessage is non-blocking MUST not be called after calling close()

func (*Client) Start

func (c *Client) Start(ctx context.Context) error

Start the client It establishes a connection to the server and starts the loops to handle incoming and outgoing messages

func (*Client) Stop

func (c *Client) Stop(ctx context.Context) error

Stop stops the connection

It attempts to gracefully stop the connection by sending a Websocket Close message to the server and waiting for the server to respond with a Close message in return and finally closes the underlying connection.

If the context is canceled before, the underlying connection is abruptly closed and stop returns as soon as its internal goroutines have finished and the its state is cleaned up.

Users SHOULD call Stop to ensure that the connection is properly closed and resources are released. Users MUST NOT call SendMessage after calling Stop.

type ClientConfig

type ClientConfig struct {
	Dialer *DialerConfig

	// PingInterval is the interval at which the client sends ping messages to the server
	PingInterval        time.Duration
	PongTimeout         time.Duration
	ReadTimeout         time.Duration
	WriteTimeout        time.Duration
	WriteControlTimeout time.Duration
}

ClinetConfig is the configuration for the websocket client

func (*ClientConfig) SetDefault

func (cfg *ClientConfig) SetDefault() *ClientConfig

type Dialer

type Dialer interface {
	DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*websocket.Conn, *http.Response, error)
}

Dialer is an interface for dialing a websocket connection.

func NewDialer

func NewDialer(cfg *DialerConfig) (dialer Dialer)

NewDialer creates a new websocket.Dialer with the given configuration.

type DialerConfig

type DialerConfig struct {
	Dialer *comnet.DialerConfig

	HandshakeTimeout                time.Duration // HandshakeTimeout specifies the duration for the handshake to complete.
	ReadBufferSize, WriteBufferSize int           // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes.
	MessageSizeLimit                *int64        // nil default, 0 no limit
	EnableCompression               bool          // EnableCompression specifies if the client should attempt to negotiate per message compression (RFC 7692)

	ReadLimit int64       // ReadLimit specifies the maximum size in bytes for a message read from the peer. If a message exceeds the limit, the connection sends a close message to the peer and returns ErrReadLimit.
	Header    http.Header // Custom headers to be attached to the request opening the WebSocket connection
}

DialerConfig is a configuration for a websocket.Dialer.

func (*DialerConfig) SetDefault

func (cfg *DialerConfig) SetDefault() *DialerConfig

SetDefault sets default values for DialerConfig

type DialerDecorator

type DialerDecorator func(Dialer) Dialer

DialerDecorator is a function that decorates a Dialer.

func WithBaseURL

func WithBaseURL(baseURL *url.URL) DialerDecorator

WithBaseURL is a DialerDecorator that sets the base URL for the websocket connection.

func WithError

func WithError() DialerDecorator

WithAuth is a DialerDecorator that wraps a websocket dialing error in an expresive error

func WithHeaders

func WithHeaders(headers http.Header) DialerDecorator

WithHeaders is a DialerDecorator that sets the headers for the websocket connection.

func WithReadLimit

func WithReadLimit(limit int64) DialerDecorator

WithReadLimit is a DialerDecorator that sets the read limit for the websocket connection.

type DialerFunc

type DialerFunc func(ctx context.Context, urlStr string, requestHeader http.Header) (*websocket.Conn, *http.Response, error)

DialerFunc is a function that implements the Dialer interface.

func (DialerFunc) DialContext

func (f DialerFunc) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*websocket.Conn, *http.Response, error)

DialContext dials a websocket connection.

type IncomingMessage

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

IncomingMessage represents a message received from the server

func (*IncomingMessage) Err

func (m *IncomingMessage) Err() error

func (*IncomingMessage) MsgType

func (m *IncomingMessage) MsgType() int

func (*IncomingMessage) Value

func (m *IncomingMessage) Value() interface{}

Jump to

Keyboard shortcuts

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