ws

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MsgClient = 16
)

Variables

View Source
var (
	ErrorHttpTimeout    = SpxError{100, "we did not get a response from the server.", "http timeout"}
	ErrorJWTInvalid     = SpxError{101, "invalid API security credentials", "missing or invalid jwt access token"}
	ErrorInvalidRequest = SpxError{102, "unexpected request parameters", "invalid parameters"}
	ErrorTimeout        = SpxError{103, "operation timeout", "operation timeout"}
	ErrorClosed         = SpxError{104, "socket closed", "underlying fd/socket closed normally"}
	ErrorInternal       = SpxError{999, "unexpected error", "internal error"}
)
View Source
var (
	NoResponse      = WSMsg{0x00, msgNoResponse}
	AutoRedial bool = true
)
View Source
var (
	LogAll bool
)

Functions

func WebSocketHandler

func WebSocketHandler(handler WSServer) http.HandlerFunc

Types

type SpxError

type SpxError struct {
	Code   int    `json:"code"`
	Text   string `json:"text"`
	Detail string `json:"error"`
}

func (SpxError) AddError

func (e SpxError) AddError(err error) SpxError

TODO: Adderror should return a new error with the added extension

func (SpxError) Error

func (e SpxError) Error() string

Implement error interface

type WSClient

type WSClient interface {
	BeforeRedial()
	AfterRedial(wsConn *WSConn)
	Process(clientId string, msg WSMsg) (response WSMsg, err error)
	Closed(wsConn *WSConn)
}

type WSConn

type WSConn struct {
	RemoteIP net.IP // store the client public IP on the server side
	ClientId string // store the device id on the server side
	// contains filtered or unexported fields
}

func GetWebSocketByClientId

func GetWebSocketByClientId(clientId string) (wsConn *WSConn)

func GetWebSocketByRemoteIP

func GetWebSocketByRemoteIP(ip net.IP) (wsConn *WSConn)

func WebSocketDial

func WebSocketDial(url url.URL, clientId string, handler WSClient) (wsConn *WSConn, err error)

WebSocketDial opens a new web socket connection

func (*WSConn) Close

func (wsConn *WSConn) Close()

Close will terminate the connection with the remote peer. This is the normal scenario. This informs the peer of a normal closure.

func (*WSConn) RPC

func (wsConn *WSConn) RPC(msgType uint8, token *string, in interface{}, out interface{}) (err error)

func (*WSConn) ServerConnIsAlive

func (wsConn *WSConn) ServerConnIsAlive() bool

func (*WSConn) WaitResponse

func (wsConn *WSConn) WaitResponse(timeout time.Duration) (msg WSMsg, err error)

func (*WSConn) Write

func (wsConn *WSConn) Write(msg WSMsg) (seq uint8, err error)

func (*WSConn) WriteAndWaitResponse

func (wsConn *WSConn) WriteAndWaitResponse(msg WSMsg) (response WSMsg, err error)

type WSMsg

type WSMsg []byte

WSMsg is a slice, so pass it by value to all

func Encode

func Encode(msgType uint8, token *string, data interface{}) (msg WSMsg, err error)

Encode will add the data struct to the msg; It uses json encoding

func EncodeBytes

func EncodeBytes(msgType uint8, token *string, data []byte) (msg WSMsg, err error)

EncodeBytes will add the raw []byte to the msg

func EncodeResponse

func EncodeResponse(old WSMsg, data interface{}) (msg WSMsg, err error)

EncodeResponse will reuse previous message

func (WSMsg) Data

func (w WSMsg) Data() []byte

func (WSMsg) Decode

func (w WSMsg) Decode(token *string, data interface{}) error

func (WSMsg) IsResponse

func (w WSMsg) IsResponse() bool

func (WSMsg) Sequence

func (w WSMsg) Sequence() uint8

func (WSMsg) Type

func (w WSMsg) Type() uint8

type WSServer

type WSServer interface {
	Accept(wsConn *WSConn) error
	// Process(msg *WSMsg) error
	WSClient
}

Jump to

Keyboard shortcuts

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