Documentation
¶
Overview ¶
Package recws provides websocket client based on gorilla/websocket that will automatically reconnect if the connection is dropped.
Index ¶
- Constants
- Variables
- func SetLogLevel(level LogLevel)
- type LogLevel
- type RecConn
- func (rc *RecConn) Close()
- func (rc *RecConn) CloseAndReconnect()
- func (rc *RecConn) Dial(urlStr string, reqHeader http.Header)
- func (rc *RecConn) GetDialError() error
- func (rc *RecConn) GetHTTPResponse() *http.Response
- func (rc *RecConn) GetURL() string
- func (rc *RecConn) IsConnected() bool
- func (rc *RecConn) ReadJSON(v interface{}) error
- func (rc *RecConn) ReadMessage() (messageType int, message []byte, err error)
- func (rc *RecConn) SetTLSClientConfig(tlsClientConfig *tls.Config)
- func (rc *RecConn) Shutdown(writeWait time.Duration)
- func (rc *RecConn) WriteJSON(v interface{}) error
- func (rc *RecConn) WriteMessage(messageType int, data []byte) error
Constants ¶
const ( // Standard log levels from slog package LogLevelDebug = slog.LevelDebug LogLevelInfo = slog.LevelInfo LogLevelWarn = slog.LevelWarn LogLevelError = slog.LevelError )
Variables ¶
var ErrNotConnected = errors.New("websocket: not connected")
ErrNotConnected is returned when the application read/writes a message and the connection is closed
Functions ¶
func SetLogLevel ¶
func SetLogLevel(level LogLevel)
SetLogLevel sets the logging level for the package
Types ¶
type RecConn ¶
type RecConn 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
// HandshakeTimeout specifies the duration for the handshake to complete,
// default to 2 seconds
HandshakeTimeout time.Duration
// Proxy specifies the proxy function for the dialer
// defaults to ProxyFromEnvironment
Proxy func(*http.Request) (*url.URL, error)
// Client TLS config to use on reconnect
TLSClientConfig *tls.Config
// SubscribeHandler fires after the connection successfully establish.
SubscribeHandler func() error
// KeepAliveTimeout is an interval for sending ping/pong messages
// disabled if 0
KeepAliveTimeout time.Duration
// LogLevel controls the verbosity of logging, defaults to LogLevelInfo
LogLevel LogLevel
// Compression enables per-message compression as defined in https://datatracker.ietf.org/doc/html/rfc7692
Compression bool
*websocket.Conn
// contains filtered or unexported fields
}
The RecConn type represents a Reconnecting WebSocket connection.
func (*RecConn) Close ¶
func (rc *RecConn) Close()
Close closes the underlying network connection without sending or waiting for a close frame.
func (*RecConn) CloseAndReconnect ¶
func (rc *RecConn) CloseAndReconnect()
CloseAndReconnect will try to reconnect.
func (*RecConn) GetDialError ¶
GetDialError returns the last dialer error. nil on successful connection.
func (*RecConn) GetHTTPResponse ¶
GetHTTPResponse returns the http response from the handshake. Useful when WebSocket handshake fails, so that callers can handle redirects, authentication, etc.
func (*RecConn) IsConnected ¶
IsConnected returns the WebSocket connection state
func (*RecConn) ReadMessage ¶
ReadMessage is a helper method for getting a reader using NextReader and reading from that reader to a buffer.
If the connection is closed ErrNotConnected is returned
func (*RecConn) SetTLSClientConfig ¶
func (*RecConn) Shutdown ¶
Shutdown gracefully closes the connection by sending the websocket.CloseMessage. The writeWait param defines the duration before the deadline of the write operation is hit.