ws

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthCb

type AuthCb func(w http.ResponseWriter, r *http.Request) (string, 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) 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 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
}

func (ClientConfig) IsValid

func (c ClientConfig) IsValid() error

type ClientOption

type ClientOption func(c *Client) 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