wsutil

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertErrorToStatusError

func ConvertErrorToStatusError(err error) error

ConvertErrorToStatusError converts an error (such as a CloseError) into a gRPC Status Error.

func MessageTypeFromProto

func MessageTypeFromProto(t roxy_v0.WebSocketFrame_Type) int

MessageTypeFromProto converts a roxy_v0.WebSocketFrame_Type enum to a gorilla/websocket message type integer.

func MessageTypeToProto

func MessageTypeToProto(messageType int) roxy_v0.WebSocketFrame_Type

MessageTypeToProto converts a gorilla/websocket message type integer to a roxy_v0.WebSocketFrame_Type enum.

Types

type Adaptor

type Adaptor struct {
	// contains filtered or unexported fields
}

Adaptor wraps a websocket.Conn so that it can be used as a gRPC-like WebSocketConn suitable for use with Looper.

func MakeAdaptor

func MakeAdaptor(conn *websocket.Conn, opts ...AdaptorOption) Adaptor

MakeAdaptor builds and returns an Adaptor.

func (Adaptor) Recv

func (adaptor Adaptor) Recv() (*roxy_v0.WebSocketFrame, error)

Recv fulfills the WebSocketConn interface.

func (Adaptor) Send

func (adaptor Adaptor) Send(frame *roxy_v0.WebSocketFrame) error

Send fulfills the WebSocketConn interface.

type AdaptorOption

type AdaptorOption func(*adaptorOptions)

AdaptorOption represents an option for building an Adaptor.

func WithReadLimit

func WithReadLimit(limit int64) AdaptorOption

WithReadLimit specifies the maximum number of bytes to read per message for an Adaptor. See the websocket.Conn.SetReadLimit method for more information.

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) AdaptorOption

WithReadTimeout specifies the connection read timeout for an Adaptor. See the websocket.Conn.SetReadDeadline method for more information.

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) AdaptorOption

WithWriteTimeout specifies the connection write timeout for an Adaptor. See the websocket.Conn.SetWriteDeadline method for more information.

type BinaryHandlerFunc

type BinaryHandlerFunc func(context.Context, *Looper, []byte)

BinaryHandlerFunc is the type for an OnBinary handler.

type CloseError

type CloseError struct {
	Code uint16
	Text string
}

CloseError represents a WebSocket connection close event.

func CloseErrorFromBytes

func CloseErrorFromBytes(p []byte) CloseError

CloseErrorFromBytes parses a CloseError from the payload bytes of a WebSocket CLOSE control message.

func (CloseError) AsBytes

func (err CloseError) AsBytes() []byte

AsBytes renders this CloseError as bytes suitable for a CLOSE control message's payload.

func (CloseError) Error

func (err CloseError) Error() string

Error fulfills the error interface.

func (CloseError) GRPCStatus

func (err CloseError) GRPCStatus() *status.Status

GRPCStatus returns a gRPC Status which is roughly equivalent to this CloseError. The details of the original CloseError are stored in the Details field of the Status proto message, as type "roxy.v0.WebSocketCloseError".

func (CloseError) Is

func (err CloseError) Is(other error) bool

Is returns true if this CloseError is equivalent to the given error.

Currently this method returns true in only two cases:

  1. If this error has code 1000 (normal closure) and other is io.EOF.

  2. If other is a gRPC Status Error with roxy_v0.WebSocketCloseError in its Details, and that roxy_v0.WebSocketCloseError has identical code and text to this error.

type CloseHandlerFunc

type CloseHandlerFunc func(context.Context, *Looper, CloseError)

CloseHandlerFunc is the type for an OnClose handler.

type Looper

type Looper struct {
	// contains filtered or unexported fields
}

Looper is a construct for handling the mid-level details of operating a WebSocket-over-gRPC endpoint (client or server).

func NewLooper

func NewLooper(ctx context.Context, conn WebSocketConn, opts ...LooperOption) *Looper

NewLooper constructs a new Looper for the given WebSocketConn.

func (*Looper) SendBinaryMessage

func (looper *Looper) SendBinaryMessage(data []byte)

SendBinaryMessage sends a BINARY data message.

It's safe to call this from any thread.

func (*Looper) SendClose

func (looper *Looper) SendClose(code uint16, text string)

SendClose sends a CLOSE control message.

It's safe to call this from any thread.

func (*Looper) SendPing

func (looper *Looper) SendPing(text string)

SendPing sends a PING control message.

It's safe to call this from any thread.

You normally don't need to call this yourself.

func (*Looper) SendPong

func (looper *Looper) SendPong(text string)

SendPong sends a PONG control message.

It's safe to call this from any thread.

You normally don't need to call this yourself.

func (*Looper) SendTextMessage

func (looper *Looper) SendTextMessage(text string)

SendTextMessage sends a TEXT data message.

It's safe to call this from any thread.

func (*Looper) Wait

func (looper *Looper) Wait() error

Wait blocks until the WebSocket connection shuts down, then returns the error which caused the connection to shut down, or nil if the connection was closed cleanly by a CLOSE control message with code 1000.

type LooperOption

type LooperOption func(*looperOptions)

LooperOption represents an option for constructing a Looper.

func OnBinary

func OnBinary(handler BinaryHandlerFunc) LooperOption

OnBinary specifies a handler for BINARY data messages.

func OnClose

func OnClose(handler CloseHandlerFunc) LooperOption

OnClose specifies a handler for CLOSE control messages.

CLOSE messages will always be automatically reflected to the peer, regardless of whether or not an OnClose handler is specified and regardless of what actions the OnClose handler takes.

func OnPing

func OnPing(handler PingHandlerFunc) LooperOption

OnPing specifies a handler for PING control messages.

PING messages will always be automatically reflected to the peer as PONG messages, regardless of whether or not an OnPing handler is specified and regardless of what actions the OnPing handler takes.

func OnPong

func OnPong(handler PongHandlerFunc) LooperOption

OnPong specifies a handler for PONG control messages.

PONG messages will be automatically processed, regardless of whether or not an OnPong handler is specified and regardless of what actions the OnPong handler takes.

func OnText

func OnText(handler TextHandlerFunc) LooperOption

OnText specifies a handler for TEXT data messages.

func WithPingInterval

func WithPingInterval(interval time.Duration) LooperOption

WithPingInterval specifies the time interval between the receiving of a PONG control message and the sending of the next automatic PING control message.

func WithPongInterval

func WithPongInterval(interval time.Duration) LooperOption

WithPongInterval specifies the time interval between the sending of an automatic PING control message and abandoning the connection because no PONG control message was received in response.

type PingHandlerFunc

type PingHandlerFunc func(context.Context, *Looper, string)

PingHandlerFunc is the type for an OnPing handler.

type PongHandlerFunc

type PongHandlerFunc func(context.Context, *Looper, string)

PongHandlerFunc is the type for an OnPong handler.

type TextHandlerFunc

type TextHandlerFunc func(context.Context, *Looper, string)

TextHandlerFunc is the type for an OnText handler.

type WebSocketConn

type WebSocketConn interface {
	Send(*roxy_v0.WebSocketFrame) error
	Recv() (*roxy_v0.WebSocketFrame, error)
}

WebSocketConn is the minimum subset of the roxy_v0.Web_SocketClient and roxy_v0.Web_SocketServer gRPC streams which is needed by Looper.

Implementations may optionally implement interface { CloseSend() error } as well, in which case CloseSend() will be called immediately after sending a CLOSE control message.

Jump to

Keyboard shortcuts

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