Documentation ¶
Overview ¶
Package which contains a WebsocketConnectionAdapterInterface implementation for gorilla/websocket library (https://github.com/gorilla/websocket).
Index ¶
- type GorillaWebsocketConnectionAdapter
- func (adapter *GorillaWebsocketConnectionAdapter) Close(ctx context.Context, code wsconnadapter.StatusCode, reason string) error
- func (adapter *GorillaWebsocketConnectionAdapter) Dial(ctx context.Context, target url.URL) (*http.Response, error)
- func (adapter *GorillaWebsocketConnectionAdapter) GetUnderlyingWebsocketConnection() any
- func (adapter *GorillaWebsocketConnectionAdapter) Ping(ctx context.Context) error
- func (adapter *GorillaWebsocketConnectionAdapter) Read(ctx context.Context) (wsconnadapter.MessageType, []byte, error)
- func (adapter *GorillaWebsocketConnectionAdapter) Write(ctx context.Context, msgType wsconnadapter.MessageType, msg []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GorillaWebsocketConnectionAdapter ¶
type GorillaWebsocketConnectionAdapter struct {
// contains filtered or unexported fields
}
Adapter for gorilla/websocket library
func NewGorillaWebsocketConnectionAdapter ¶
func NewGorillaWebsocketConnectionAdapter(dialer *websocket.Dialer, requestHeader http.Header) *GorillaWebsocketConnectionAdapter
Description ¶
Factory which creates a new GorillaWebsocketConnectionAdapter.
Inputs ¶
dialer: Optional dialer to use when using Dial method. If nil, the default dialer defined by gorilla library will be used.
requestHeader: Headers which will be used during Dial to specify the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie)
Returns ¶
New GorillaWebsocketConnectionAdapter
func (*GorillaWebsocketConnectionAdapter) Close ¶
func (adapter *GorillaWebsocketConnectionAdapter) Close(ctx context.Context, code wsconnadapter.StatusCode, reason string) error
Description ¶
Send a close message with the provided status code and an optional close reason and drop the websocket connection.
Inputs ¶
- ctx: Context used for tracing purpose
- code: Status code to use in close message
- reason: Optional reason joined in clsoe message. Can be empty.
Returns ¶
- nil in case of success
- error: server unreachable, connection already closed, ...
func (*GorillaWebsocketConnectionAdapter) Dial ¶
func (adapter *GorillaWebsocketConnectionAdapter) Dial(ctx context.Context, target url.URL) (*http.Response, error)
Description ¶
Dial opens a connection to the websocket server and performs a WebSocket handshake.
Inputs ¶
- ctx: Context used for tracing/timeout purpose
- target: Target server URL
Returns ¶
The server response to websocket handshake or an error if any.
func (*GorillaWebsocketConnectionAdapter) GetUnderlyingWebsocketConnection ¶
func (adapter *GorillaWebsocketConnectionAdapter) GetUnderlyingWebsocketConnection() any
Description ¶
Return the underlying websocket connection if any. Returned value has to be type asserted.
Returns ¶
The underlying websocket connection if any. Returned value has to be type asserted.
func (*GorillaWebsocketConnectionAdapter) Ping ¶
func (adapter *GorillaWebsocketConnectionAdapter) Ping(ctx context.Context) error
Description ¶
Send a ping message to the websocket server and block until a pong response is received, the connection is closed, or the provided context is cancelled.
A separate goroutine must continuously call the Read method to process messages from the server so that pong responses from the server can be processed.
Inputs ¶
- ctx: context used for tracing/timeout purpose.
Returns ¶
- nil in case of success: A Ping message has been sent to the server and a Pong has been received. - error: connection is closed, context timeout/cancellation, ...
func (*GorillaWebsocketConnectionAdapter) Read ¶
func (adapter *GorillaWebsocketConnectionAdapter) Read(ctx context.Context) (wsconnadapter.MessageType, []byte, error)
Description ¶
Read a single message from the websocket server. Read blocks until a message is received from the server, or until connection is closed.
Read will handle control frames from the server until a message is received:
- Ping from server are discarded.
- Close will result in a wsconnadapter.WebsocketCloseError for Read and all pending Ping.
- Each pong message will be used to unlock one pending Ping call.
Inputs ¶
- ctx: Context used for tracing purpose
Returns ¶
- MessageType: received message type (Binary | Text)
- []bytes: Message content
- error: in case of connection closure, context timeout/cancellation or failure.
func (*GorillaWebsocketConnectionAdapter) Write ¶
func (adapter *GorillaWebsocketConnectionAdapter) Write(ctx context.Context, msgType wsconnadapter.MessageType, msg []byte) error
Description ¶
Write a single message to the websocket server. Write blocks until message is sent to the server or until an error occurs: context timeout, cancellation, connection closed, ....
Inputs ¶
- ctx: Context used for tracing/timeout purpose
- MessageType: received message type (Binary | Text)
- []bytes: Message content
Returns ¶
- error: in case of connection closure, context timeout/cancellation or failure.