websocket

package
v0.0.0-...-2649ce0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotConnected = errors.New("websocket: not connected")

ErrNotConnected is returned when the application read/writes a message and the connection is closed

Functions

This section is empty.

Types

type Client

type Client struct {
	// RecIntvlMin specifies the initial reconnecting interval,
	// default to 2 seconds.
	RecIntvlMin time.Duration

	// RecIntvlMax specifies the maximum reconnecting interval,
	// default to 30 seconds.
	RecIntvlMax time.Duration

	// RecIntvlFactor specifies the rate of increase of the reconnection
	// interval, default to 1.5.
	RecIntvlFactor float64

	// Backoff holds the previous attributes in order to allow the reconnection
	// mechanism to work.
	Backoff *backoff.Backoff

	// HandshakeTimeout specifies the duration for the handshake to complete,
	// default to 2 seconds.
	HandshakeTimeout time.Duration

	// Verbose suppress connecting/reconnecting messages.
	Verbose bool

	// Conn represents the underlying websocket connection.
	Conn *websocket.Conn

	// Dialer represents the mechanism used to perform the connection.
	Dialer *websocket.Dialer

	// OnConnected represents the handler dispatched when the connection is opened.
	OnConnected OnConnectedHandler

	// OnMessage represents the handler dispatched when a message is read.
	OnMessage OnMessageHandler
	// contains filtered or unexported fields
}

Client type represents a Reconnecting WebSocket connection.

func New

func New(url string, logger log.Logger) (*Client, error)

New returns a new configured websocket client for the passed url.

func (*Client) Close

func (c *Client) Close() error

Close closes the underlying network connection without sending or waiting for a close frame.

func (*Client) CloseAndReconnect

func (c *Client) CloseAndReconnect() (err error)

CloseAndReconnect will try to reconnect.

func (*Client) Closed

func (c *Client) Closed() bool

Closed returns the WebSocket connection state

func (*Client) Connect

func (c *Client) Connect()

Connect performs the connection. This function should be executed by a goroutine.

func (*Client) Connected

func (c *Client) Connected() bool

Connected returns the WebSocket connection state

func (*Client) Dial

func (c *Client) Dial()

Dial establishes a new client connection. The URL url specifies the host and request URI. Use requestHeader to specify the origin (Origin), sub-protocols (Sec-WebSocket-Protocol) and cookies (Cookie).

func (*Client) GetBackoff

func (c *Client) GetBackoff() *backoff.Backoff

GetBackoff retrieves the backoff associated with the connection.

func (*Client) GetVerbose

func (c *Client) GetVerbose() bool

GetVerbose returns `Verbose`, a boolean used to define if the client should suppress connection/reconnection messages or not. False by default.

func (*Client) ReadJSON

func (c *Client) ReadJSON(v interface{}) (err error)

ReadJSON reads the next JSON-encoded message from the connection and stores it in the value pointed to by v.

See the documentation for the encoding/json Unmarshal function for details about the conversion of JSON to a Go value.

If the connection is closed ErrNotConnected is returned

func (*Client) ReadMessage

func (c *Client) ReadMessage() (messageType int, message []byte, err error)

ReadMessage is a helper method for reading a message from the underlying connection. If the connection is closed ErrNotConnected is returned

func (*Client) ReadMessages

func (c *Client) ReadMessages() error

ReadMessages reads messages while the connection is active

func (*Client) SetKeepAliveTimeout

func (c *Client) SetKeepAliveTimeout(interval time.Duration)

SetKeepAliveTimeout sets the interval for the keep alive message

func (*Client) WriteJSON

func (c *Client) WriteJSON(v interface{}) (err error)

WriteJSON writes the JSON encoding of v to the connection.

See the documentation for encoding/json Marshal for details about the conversion of Go values to JSON.

If the connection is closed ErrNotConnected is returned

func (*Client) WriteMessage

func (c *Client) WriteMessage(messageType int, data []byte) (err error)

WriteMessage is a helper method for writing a message to the underlying connection. If the connection is closed ErrNotConnected is returned

type Config

type Config interface {
	Auth() *auth.Authentication
	BaseUrl() string
}

type OnConnectedHandler

type OnConnectedHandler func() ([]byte, error)

OnConnectedHandler is a handler that is dispatched when the client reads a message from the websocket. This can be the authentication message and/or something else.

type OnMessageHandler

type OnMessageHandler func(message []byte) error

OnMessageHandler is a handler that is dispatched when the client reads a message from the websocket.

Jump to

Keyboard shortcuts

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