Documentation
¶
Index ¶
- func DeregisterMessageParserCallback[T any](r *messageParsers_Registry, handler *typedHandler[*T])
- func RegisterMessageParserCallback[T any](r *messageParsers_Registry, parser ParserFunc[*T], callback CallbackFunc[*T]) *typedHandler[*T]
- type CallbackFunc
- type CloseEvent
- type Error
- type EventEmitter
- type IncomingMessage
- type IncomingRequest
- type ParserFunc
- type SendRequestParams
- type ServerIncomingRequest
- type SocketCloseEvent
- type SocketError
- type SocketMessage
- type UpgradeError
- type VoidHandler
- type WebsocketClient
- func (ws_client *WebsocketClient) Close() error
- func (ws_client *WebsocketClient) Connect() error
- func (ws_client *WebsocketClient) GetRequestIdProperty() string
- func (ws_client *WebsocketClient) IsClosed() bool
- func (ws_client *WebsocketClient) SendJSON(msg interface{}) error
- func (ws_client *WebsocketClient) SendRequest(request any, response any, opt_params *SendRequestParams) (hasTimedOut bool, err error)
- func (ws_client *WebsocketClient) SendText(msg string) error
- func (ws_client *WebsocketClient) SetRequestIdProperty(propertyName string) error
- type WebsocketConnection
- func (ws_connection *WebsocketConnection) Close() error
- func (ws_connection *WebsocketConnection) SendJSON(msg interface{}) error
- func (ws_connection *WebsocketConnection) SendPreparedMessage(pm *websocket.PreparedMessage) error
- func (ws_connection *WebsocketConnection) SendRequest(request any, response any, opt_params *SendRequestParams) (hasTimedOut bool, err error)
- func (ws_connection *WebsocketConnection) SendText(msg string) error
- type WebsocketServer
- func (ws_server *WebsocketServer) GetRequestIdProperty() string
- func (ws_server *WebsocketServer) Listen(port int, paths ...string) error
- func (ws_server *WebsocketServer) SetCheckOriginFunc(checkOriginFunc func(r *http.Request) bool)
- func (ws_server *WebsocketServer) SetRequestIdProperty(propertyName string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeregisterMessageParserCallback ¶
func DeregisterMessageParserCallback[T any](r *messageParsers_Registry, handler *typedHandler[*T])
Generic function to register parser and callback for type T
if `parser` is nil, the callback will be called upon any non-error unmarshall of messages (not recommended unless it is the only message structure that is received)
func RegisterMessageParserCallback ¶
func RegisterMessageParserCallback[T any](r *messageParsers_Registry, parser ParserFunc[*T], callback CallbackFunc[*T]) *typedHandler[*T]
Generic function to register parser and callback for type T
if `parser` is nil, the callback will be called upon any non-error unmarshall of messages (not recommended unless it is the only message structure that is received)
Types ¶
type CallbackFunc ¶
type CallbackFunc[T any] func(T)
type CloseEvent ¶
type CloseEvent struct {
Connection *WebsocketConnection
Code int
Text string
}
type Error ¶
type Error struct {
Connection *WebsocketConnection
Err error
}
type EventEmitter ¶
type EventEmitter[T any] struct { // contains filtered or unexported fields }
func (*EventEmitter[T]) Emit ¶
func (e *EventEmitter[T]) Emit(arg T)
Not sure if this needs to be public
func (*EventEmitter[T]) Subscribe ¶
func (e *EventEmitter[T]) Subscribe(handler VoidHandler[T])
func (*EventEmitter[T]) Unsubscribe ¶
func (e *EventEmitter[T]) Unsubscribe(handler VoidHandler[T])
type IncomingMessage ¶
type IncomingMessage struct {
Connection *WebsocketConnection
Message []byte
}
type IncomingRequest ¶
type IncomingRequest struct {
Data []byte
// contains filtered or unexported fields
}
func (*IncomingRequest) Reply ¶
func (request *IncomingRequest) Reply(data interface{}) error
type SendRequestParams ¶
type ServerIncomingRequest ¶
type ServerIncomingRequest struct {
WebsocketConnection *WebsocketConnection
Data []byte
// contains filtered or unexported fields
}
func (*ServerIncomingRequest) Reply ¶
func (request *ServerIncomingRequest) Reply(data interface{}) error
type SocketCloseEvent ¶
type SocketError ¶
type SocketError struct {
Err error
}
type SocketMessage ¶
type SocketMessage struct {
Message []byte
}
type UpgradeError ¶
type WebsocketClient ¶
type WebsocketClient struct {
URL string
Conn *websocket.Conn
OnError EventEmitter[SocketError]
OnClose EventEmitter[SocketCloseEvent]
OnAnyMessage EventEmitter[SocketMessage]
// This is an eventEmitter for incoming requests from the server socket
OnRequest EventEmitter[IncomingRequest]
// This is an eventEmitter for incoming responses from the server to a request sent from the client
OnRequestResponse EventEmitter[SocketMessage]
// In order to register the parsed messages
MessageParserRegistry messageParsers_Registry
OnParsedMessage EventEmitter[SocketMessage]
OnMessage EventEmitter[SocketMessage]
// contains filtered or unexported fields
}
func NewWebsocketClient ¶
func NewWebsocketClient(url string) *WebsocketClient
func (*WebsocketClient) Close ¶
func (ws_client *WebsocketClient) Close() error
func (*WebsocketClient) Connect ¶
func (ws_client *WebsocketClient) Connect() error
func (*WebsocketClient) GetRequestIdProperty ¶
func (ws_client *WebsocketClient) GetRequestIdProperty() string
func (*WebsocketClient) IsClosed ¶
func (ws_client *WebsocketClient) IsClosed() bool
func (*WebsocketClient) SendJSON ¶
func (ws_client *WebsocketClient) SendJSON(msg interface{}) error
func (*WebsocketClient) SendRequest ¶
func (ws_client *WebsocketClient) SendRequest(request any, response any, opt_params *SendRequestParams) (hasTimedOut bool, err error)
SendRequest sends msg to the server and waits for a response or timeout (if set).
The response is unmarshalled into the provided variable v.
If the message failed to send, or the response is not received within the timeout, an error is returned along with the respective hasTimedOut value.
func (*WebsocketClient) SendText ¶
func (ws_client *WebsocketClient) SendText(msg string) error
func (*WebsocketClient) SetRequestIdProperty ¶
func (ws_client *WebsocketClient) SetRequestIdProperty(propertyName string) error
type WebsocketConnection ¶
type WebsocketConnection struct {
Id int64 // Read-only unique ID for the connection (set automatically by the server)
Conn *websocket.Conn
OrigRequest *http.Request
OnError EventEmitter[SocketError]
OnClose EventEmitter[SocketCloseEvent]
OnAnyMessage EventEmitter[SocketMessage]
// This is an eventEmitter for incoming requests from the client socket
OnRequest EventEmitter[IncomingRequest]
// This is an eventEmitter for incoming response from the client socket of a request sent from the server
OnRequestResponse EventEmitter[SocketMessage]
OnParsedMessage EventEmitter[SocketMessage]
OnMessage EventEmitter[SocketMessage]
// # Not used by library
//
// This is simply left to the developer to use as they please
//
// In the case of complex AllowOrigin configurations, this can help store metadata to match a connection to a user or any application-logic data
CustomData struct {
Mu sync.Mutex
Map map[string]string
}
// contains filtered or unexported fields
}
func (*WebsocketConnection) Close ¶
func (ws_connection *WebsocketConnection) Close() error
func (*WebsocketConnection) SendJSON ¶
func (ws_connection *WebsocketConnection) SendJSON(msg interface{}) error
func (*WebsocketConnection) SendPreparedMessage ¶ added in v1.1.2
func (ws_connection *WebsocketConnection) SendPreparedMessage(pm *websocket.PreparedMessage) error
func (*WebsocketConnection) SendRequest ¶
func (ws_connection *WebsocketConnection) SendRequest(request any, response any, opt_params *SendRequestParams) (hasTimedOut bool, err error)
SendRequest sends msg to the server and waits for a response or timeout (if set).
The response is unmarshalled into the provided variable v.
If the message failed to send, or the response is not received within the timeout, an error is returned along with the respective hasTimedOut value.
func (*WebsocketConnection) SendText ¶
func (ws_connection *WebsocketConnection) SendText(msg string) error
type WebsocketServer ¶
type WebsocketServer struct {
Clients struct {
Mu sync.Mutex
Map map[int64]*WebsocketConnection
}
OnUpgradeError EventEmitter[UpgradeError]
OnConnect EventEmitter[*WebsocketConnection]
OnError EventEmitter[Error]
OnClose EventEmitter[CloseEvent]
OnAnyMessage EventEmitter[IncomingMessage]
// This is an eventEmitter for incoming requests from the client socket
OnRequest EventEmitter[ServerIncomingRequest]
// This is an eventEmitter for incoming response from the client socket of a request sent from the server
OnRequestResponse EventEmitter[IncomingMessage]
// In order to register the parsed messages
MessageParserRegistry messageParsers_Registry
OnParsedMessage EventEmitter[IncomingMessage]
OnMessage EventEmitter[IncomingMessage]
// contains filtered or unexported fields
}
func NewWebSocketServer ¶
func NewWebSocketServer(checkOriginFunc func(r *http.Request) bool) *WebsocketServer
Create a function to create a new websocket server, with a given address and port
func (*WebsocketServer) GetRequestIdProperty ¶
func (ws_server *WebsocketServer) GetRequestIdProperty() string
func (*WebsocketServer) Listen ¶
func (ws_server *WebsocketServer) Listen(port int, paths ...string) error
'Listen' starts the WebSocket server and listens for incoming connections on the specified paths.
This function is blocking and will not return until the server is stopped.
If no paths are provided, it will listen on the root path ("/").
func (*WebsocketServer) SetCheckOriginFunc ¶
func (ws_server *WebsocketServer) SetCheckOriginFunc(checkOriginFunc func(r *http.Request) bool)
func (*WebsocketServer) SetRequestIdProperty ¶
func (ws_server *WebsocketServer) SetRequestIdProperty(propertyName string) error