connection

package
v0.4.101 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package connection provides a Websocket that will automatically reconnect if the connection is dropped.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotDialed is returned when WriteMessage is called, but
	// the websocket has not been created yet (call Dial).
	ErrNotDailed = errors.New("websocket not created yet, please call Dial()")
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// InitialInterval is the first interval at which the backoff starts
	// running.
	InitialInterval time.Duration
	// RandomizationFactor is used to create the range of values:
	// [currentInterval - randomizationFactor * currentInterval,
	// currentInterval + randomizationFactor * currentInterval] and picking
	// a random value from the range.
	RandomizationFactor float64
	// Multiplier is used to increment the backoff interval by multiplying it.
	Multiplier float64
	// MaxInterval is an interval such that, once reached, the backoff will
	// retry with a constant delay of MaxInterval.
	MaxInterval time.Duration
	// MaxElapsedTime is the amount of time after which the ExponentialBackOff
	// returns Stop. It never stops if MaxElapsedTime == 0.
	MaxElapsedTime time.Duration
	// DialMessage is the message sent when the connection is started.
	DialMessage interface{}
	// contains filtered or unexported fields
}

Conn contains the state needed to connect, reconnect, and send messages. Default values must be updated before calling `Dial`.

func NewConn

func NewConn() *Conn

NewConn creates a new Conn with default values.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the network connection and cleans up private resources after the connection is done.

func (*Conn) Dial

func (c *Conn) Dial(address string, header http.Header, dialMsg interface{}) error

Dial creates a new persistent client connection and sets the necessary state for future reconnections. It also starts a goroutine to reset the number of reconnections.

A call to Dial is a prerequisite to writing any messages. The function only needs to be called once on start to create the connection. Alternatively, if Close is called, Dial will have to be called again if the connection needs to be recreated.

The function returns an error if the url is invalid or if a 4XX error (except 408 and 425) is received in the HTTP response.

func (*Conn) IsConnected

func (c *Conn) IsConnected() bool

IsConnected returns the WebSocket connection state.

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data interface{}) error

WriteMessage sends the JSON encoding of `data` as a message. If the write fails or a disconnect has been detected, it will close the connection and try to reconnect and resend the message.

The write will fail under the following conditions:

  1. The client has not called Dial (ErrNotDialed).
  2. The connection is disconnected and it was not able to reconnect.
  3. The write call in the websocket package failed (gorilla/websocket error).

Jump to

Keyboard shortcuts

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