wsdialer

package
v0.6.19 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeout of websocker dialer
	DefaultTimeout = 30 * time.Second
)

Variables

View Source
var (
	// DefaultBackoff of reconnection
	DefaultBackoff = []float64{0.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0}
)

Functions

This section is empty.

Types

type DisconnectError

type DisconnectError struct {
	Type string
}

DisconnectError is sent on websocket disconnect

func (DisconnectError) Error

func (err DisconnectError) Error() string

Error implements error interface

type ReConnectSettings

type ReConnectSettings struct {
	// AutoReconnect, set to automatically try to reconnect when disconnected, will be set to false on "Close"
	AutoReconnect bool
	// Backoff scheme in seconds for reconnecting websocket if "AutoReconnect" set to true. Defaults to
	Backoff []float64
	// OnReconnectStart triggers when reconnect start (only with AutoReconnect set)
	OnReconnectStart func()
	// OnReconnectDone triggers when reconnect is considered successful or failed
	// err : error during last attempt (nil of successful)
	// attempts: amount of attempts including successful attempt tried
	// timeSpent: total duration spent trying to re-connect
	OnReconnectDone func(err error, attempts int, timeSpent time.Duration)
	// GetContext context used to abort waiting during backoff and as a mother context to dial, defaults to background context
	GetContext func() context.Context
}

ReConnectSettings settings for automatically reconnecting websocket

type WsDialer

type WsDialer struct {
	gobwas.Dialer
	net.Conn
	// Type of websocket, will be used by DisconnectError
	Type string
	// Reconnect settings
	Reconnect ReConnectSettings
	// OnUnexpectedDisconnect triggers on disconnect of websocket
	OnUnexpectedDisconnect func()
	// contains filtered or unexported fields
}

WsDialer wraps gobwas websocket dialer

func New

func New(url *neturl.URL, httpHeader http.Header, cookieJar http.CookieJar, timeout time.Duration, allowUntrusted bool, wstype string) (*WsDialer, error)

New Create new websocket dialer, use type to define a specific type which would be reported when getting a DisconnectError

func (*WsDialer) Close

func (dialer *WsDialer) Close() error

Close connection

func (*WsDialer) Closed

func (dialer *WsDialer) Closed() <-chan struct{}

Closed returns chan which will be closed when Close() is triggered

func (*WsDialer) Dial

func (dialer *WsDialer) Dial(ctx context.Context) error

Dial new websocket connection

func (*WsDialer) IsClosed

func (dialer *WsDialer) IsClosed() bool

IsClosed check if Close() has been triggered

func (*WsDialer) ReadMessage

func (dialer *WsDialer) ReadMessage() (int, []byte, error)

ReadMessage Read one entire message from websocket

func (*WsDialer) WriteMessage

func (dialer *WsDialer) WriteMessage(messageType int, data []byte) error

WriteMessage Write message to a frame on the websocket

Jump to

Keyboard shortcuts

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