websocket

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package websocket is a generated GoMock package.

Index

Constants

View Source
const (
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	TextMessage = 1
)

The message types are defined in RFC 6455, section 11.8.

Variables

View Source
var ErrorConnection = errors.New("couldn't establish connection to web socket")

ErrorConnection is the connection error that occurs when webscoket connection cannot be established.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	*websocket.Conn
}

Connection is a wrapper for gorilla websocket connection.

func (*Connection) Bind

func (w *Connection) Bind(v interface{}) error

func (*Connection) Context

func (*Connection) Context() context.Context

func (*Connection) HostName

func (*Connection) HostName() string

func (*Connection) Param

func (*Connection) Param(_ string) string

func (*Connection) Params

func (*Connection) Params(string) []string

func (*Connection) PathParam

func (*Connection) PathParam(_ string) string

type ConnectionHub

type ConnectionHub struct {
	WebSocketConnections map[string]*Connection
	// contains filtered or unexported fields
}

ConnectionHub stores and provide functionality to work with all active connections with websocket clients.

type Manager

type Manager struct {
	ConnectionHub
	WebSocketUpgrader *WSUpgrader
}

Manager is a websocket manager that handles the upgrader and manages all active connections through ConnectionHub.

func New

func New() *Manager

New intializes a new websocket manager with default websocket upgrader.

func (*Manager) AddWebsocketConnection

func (ws *Manager) AddWebsocketConnection(connID string, conn *Connection)

AddWebsocketConnection add a new connection with the connection id key.

func (*Manager) CloseConnection

func (ws *Manager) CloseConnection(connID string)

CloseConnection closes a websocket connection and then removes it from the connection hub.

func (*Manager) GetWebsocketConnection

func (ws *Manager) GetWebsocketConnection(connID string) *Connection

GetWebsocketConnection returns a websocket connection which has been intialized in the middleware.

type MockUpgrader

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

MockUpgrader is a mock of Upgrader interface.

func NewMockUpgrader

func NewMockUpgrader(ctrl *gomock.Controller) *MockUpgrader

NewMockUpgrader creates a new mock instance.

func (*MockUpgrader) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockUpgrader) Upgrade

func (m *MockUpgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)

Upgrade mocks base method.

type MockUpgraderMockRecorder

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

MockUpgraderMockRecorder is the mock recorder for MockUpgrader.

func (*MockUpgraderMockRecorder) Upgrade

func (mr *MockUpgraderMockRecorder) Upgrade(w, r, responseHeader any) *gomock.Call

Upgrade indicates an expected call of Upgrade.

type Options

type Options func(u *websocket.Upgrader)

Options is a function type that applies a configuration to the concrete Upgrader.

func WithCheckOrigin

func WithCheckOrigin(fn func(r *http.Request) bool) Options

WithCheckOrigin sets the CheckOrigin handler option.

func WithCompression

func WithCompression() Options

WithCompression enables compression.

func WithError

func WithError(fn func(w http.ResponseWriter, r *http.Request, status int, reason error)) Options

WithError sets the Error handler option.

func WithHandshakeTimeout

func WithHandshakeTimeout(t time.Duration) Options

WithHandshakeTimeout sets the HandshakeTimeout option.

func WithReadBufferSize

func WithReadBufferSize(size int) Options

WithReadBufferSize sets the ReadBufferSize option.

func WithSubprotocols

func WithSubprotocols(subprotocols ...string) Options

WithSubprotocols sets the Subprotocols option.

func WithWriteBufferSize

func WithWriteBufferSize(size int) Options

WithWriteBufferSize sets the WriteBufferSize option.

type Upgrader

type Upgrader interface {
	Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)
}

Upgrader interface for upgrading HTTP connections to WebSocket connections.

type WSKey

type WSKey string

WSKey defines the key type for WSConnectionKey.

const WSConnectionKey WSKey = "ws-connection-key"

WSConnectionKey is a key constant that stores the connection id in the request context.

type WSUpgrader

type WSUpgrader struct {
	Upgrader Upgrader
}

func NewWSUpgrader

func NewWSUpgrader(opts ...Options) *WSUpgrader

NewWSUpgrader initialize a new websocket upgarder that upgrades an incoming http request to a websocket connection. It takes in Options that can be used to customize the upgraded connections.

func (*WSUpgrader) Upgrade

func (u *WSUpgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)

Upgrade calls the upgrader to upgrade an http connection to a websocket connection.

Jump to

Keyboard shortcuts

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