ws

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: AGPL-3.0, Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WSConnClosed int32 = iota
	WSConnOpen
	WSConnClosing
)
View Source
const (
	ReceiveChSize = 5000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthCb

type AuthCb func(w http.ResponseWriter, r *http.Request) (string, int, error)

type Client

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

func NewClient

func NewClient(cfg ClientConfig, opts ...ClientOption) (*Client, error)

NewClient initializes and returns a new WebSocket client.

func (*Client) Close

func (c *Client) Close() error

Close closes the underlying WebSocket connection.

func (*Client) ErrorCh

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

ErrorCh returns a channel that is used to receive client errors asynchronously.

func (*Client) GetConnState added in v0.15.0

func (c *Client) GetConnState() int32

func (*Client) ReceiveCh

func (c *Client) ReceiveCh() <-chan Message

ReceiveCh returns a channel that should be used to receive messages from the underlying ws connection.

func (*Client) Send

func (c *Client) Send(mt MessageType, data []byte) error

SendMsg sends a WebSocket message with the specified type and data.

type ClientAuthType added in v0.13.0

type ClientAuthType int
const (
	BasicClientAuthType ClientAuthType = iota + 1
	BearerClientAuthType
)

type ClientConfig

type ClientConfig struct {
	// URL specifies the WebSocket URL to connect to.
	// Should start with either `ws://` or `wss://`.
	URL string
	// ConnID specifies the id of the connection to be used in case of
	// reconnection. Should be left empty on initial connect.
	ConnID string
	// AuthToken specifies the token to be used to authenticate
	// the connection.
	AuthToken string
	// AuthType specifies the type of HTTP authentication to use when connecting.
	AuthType ClientAuthType
}

func (ClientConfig) IsValid

func (c ClientConfig) IsValid() error

type ClientOption

type ClientOption func(c *Client) error

func WithDialFunc added in v0.5.0

func WithDialFunc(dialFn DialContextFn) ClientOption

WithDialFunc lets the caller set an optional dialing function to setup the TCP connection needed by the client.

func WithLogger added in v0.16.0

func WithLogger(log *slog.Logger) ClientOption

WithLogger lets the caller set an optional logger.

type DialContextFn added in v0.5.0

type DialContextFn func(ctx context.Context, network, addr string) (net.Conn, error)

type Message

type Message struct {
	ClientID string
	ConnID   string
	Type     MessageType
	Data     []byte
}

Message defines the data to be sent to or received from a ws connection.

type MessageType

type MessageType int

MessageType defines the type of message sent to or received from a ws connection.

const (
	TextMessage MessageType = iota + 1
	BinaryMessage
	OpenMessage
	CloseMessage
)

type Server

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

func NewServer

func NewServer(cfg ServerConfig, log mlog.LoggerIFace, opts ...ServerOption) (*Server, error)

NewServer initializes and returns a new WebSocket server.

func (*Server) Close

func (s *Server) Close()

Close stops the websocket server and closes all the ws connections. Must be called once all senders are done.

func (*Server) ReceiveCh

func (s *Server) ReceiveCh() <-chan Message

ReceiveCh returns a channel that can be used to receive messages from ws connections.

func (*Server) Send

func (s *Server) Send(msg Message) error

SendCh queues a message to be sent through a ws connection.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes the WebSocket server implement http.Handler so that it can be passed to a RegisterHandler method.

type ServerConfig

type ServerConfig struct {
	// ReadBufferSize specifies the size of the internal buffer
	// used to read from a ws connection.
	ReadBufferSize int
	// WriteBufferSize specifies the size of the internal buffer
	// used to wirte to a ws connection.
	WriteBufferSize int
	// PingInterval specifies the interval at which the server should send ping
	// messages to its connections. If the client doesn't respond in 2*PingInterval
	// the server will consider the client as disconnected and drop the connection.
	PingInterval time.Duration
}

func (ServerConfig) IsValid

func (c ServerConfig) IsValid() error

type ServerOption

type ServerOption func(s *Server) error

func WithAuthCb

func WithAuthCb(cb AuthCb) ServerOption

WithAuthCb lets the caller set an optional callback to be called prior to performing the WebSocket upgrade.

Jump to

Keyboard shortcuts

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