Documentation
¶
Overview ¶
Package which contains a WebsocketConnectionAdapterInterface implementation for nhooyr/websocket library (https://github.com/nhooyr/websocket).
Package which contains a WebsocketConnectionAdapterInterface implementation for nhooyr/websocket library (https://github.com/nhooyr/websocket).
Index ¶
- type NhooyrWebsocketConnectionAdapter
- func (adapter *NhooyrWebsocketConnectionAdapter) Close(ctx context.Context, code wsconnadapter.StatusCode, reason string) error
- func (adapter *NhooyrWebsocketConnectionAdapter) Dial(ctx context.Context, target *url.URL) (*http.Response, error)
- func (adapter *NhooyrWebsocketConnectionAdapter) GetUnderlyingWebsocketConnection() any
- func (adapter *NhooyrWebsocketConnectionAdapter) Ping(ctx context.Context) error
- func (adapter *NhooyrWebsocketConnectionAdapter) Read(ctx context.Context) (wsconnadapter.MessageType, []byte, error)
- func (adapter *NhooyrWebsocketConnectionAdapter) 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 NhooyrWebsocketConnectionAdapter ¶
type NhooyrWebsocketConnectionAdapter struct {
// contains filtered or unexported fields
}
Adapter for nhooyr/websocket library
func NewNhooyrWebsocketConnectionAdapter ¶
func NewNhooyrWebsocketConnectionAdapter(opts *websocket.DialOptions, tracerProvider trace.TracerProvider) *NhooyrWebsocketConnectionAdapter
Description ¶
Factory which creates a new NhooyrWebsocketConnectionAdapter.
Inputs ¶
- opts: Optional dial options to use when calling Dial method. Can be nil.
- tracerProvider: optional tracer provider to use to get a tracer. If nil, global tracer provider will be used as instead.
Returns ¶
New NhooyrWebsocketConnectionAdapter
func (*NhooyrWebsocketConnectionAdapter) Close ¶
func (adapter *NhooyrWebsocketConnectionAdapter) 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 close the websocket connection.
Expected behaviour ¶
- Close MUST be blocking until close message has been sent to the server.
- Close MUST drop pending write/read messages.
- Close MUST close the connection even if provided context is already canceled.
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 (*NhooyrWebsocketConnectionAdapter) Dial ¶
func (adapter *NhooyrWebsocketConnectionAdapter) Dial(ctx context.Context, target *url.URL) (*http.Response, error)
Description ¶
Dial open a connection to the websocket server, performs a WebSocket handshake on url and keep internally the underlying websocket connection for further use.
Expected behaviour ¶
Dial MUST block until websocket handshake completes. Websocket handshake and TLS must be handled seamlessly either by the adapter implementation or by the underlying websocket library.
Dial MUST NOT return the undelrying websocket connection. The undelrying websocket connection must be kept internally by the adapter implementation in order to be used later by Read, Write, ...
Dial MUST return an error if called while there is already an alive connection. It is up to the adapter implementation or to the underlying websocket library to detect whether a connection is already alive or not.
Inputs ¶
- ctx: Context used for tracing/timeout purpose
- target: Target server URL
Returns ¶
- Server response to websocket handshake
- error if any
func (*NhooyrWebsocketConnectionAdapter) GetUnderlyingWebsocketConnection ¶
func (adapter *NhooyrWebsocketConnectionAdapter) 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 (*NhooyrWebsocketConnectionAdapter) Ping ¶
func (adapter *NhooyrWebsocketConnectionAdapter) Ping(ctx context.Context) error
Description ¶
Send a Ping message to the websocket server and blocks until a Pong response is received.
Expected behaviour ¶
Ping MUST be blocking either until an error or a context timeout or cancellation occurs or until Ping message is sent and a Pong response is somehow detected either by the adapter implementation or by the underlying websocket connection library.
It can be assumed that there will be at least one concurrent goroutine which continuously call Read method.
Ping MUST return an error if connection is closed, if server is unreachable or if context has expired (timeout or cancel). In this later case, Ping MUST return the context error.
Inputs ¶
- ctx: context used for tracing/timeout purpose.
Returns ¶
- nil in case of success: if a Ping message is sent to the server and if a Pong is received. - error: connection is closed, context timeout/cancellation, ...
func (*NhooyrWebsocketConnectionAdapter) Read ¶
func (adapter *NhooyrWebsocketConnectionAdapter) 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, until connection closes or until a timeout or a cancel occurs.
Expected behaviour ¶
Read MUST handle seamlessly message defragmentation, decompression and TLS decryption. It is up to the adapter implementation or to the underlying websocket library to handle these features.
Read MUST NOT return close, ping, pong and continuation frames as control frames MUST be handled seamlessly either by the adapter implementation or by the underlying websocket connection library.
Read MUST return a WebsocketCloseError either if a close message is read or if connection is closed without a close message. In the later case, the 1006 status code MUST be used.
Read MUST block until a message is read from the server or until an error occurs.
Inputs ¶
- ctx: Context used for tracing/timeout purpose
Returns ¶
- MessageType: received message type. -1 in case of error.
- []bytes: Message content. Nil in case of error.
- error: in case of connection closure, context timeout/cancellation or failure.
func (*NhooyrWebsocketConnectionAdapter) Write ¶
func (adapter *NhooyrWebsocketConnectionAdapter) 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, ....
Expected behaviour ¶
Write MUST handle seamlessly message fragmentation, compression and TLS encryption. It is up to the adapter implementation or to the underlying websocket library to handle these.
Write MUST NOT handle sending control frames like Close, Ping, etc...
Write MUST be blocking until a message is sent to the server or until an error occurs.
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.