websocket

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package websocket provides a types.Transport implementation using WebSockets.

Package websocket provides a types.Transport implementation using WebSockets.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDialer = ws.Dialer{}

DefaultDialer is a gobwas/ws Dialer with default options.

View Source
var DefaultHTTPUpgrader = ws.HTTPUpgrader{}

DefaultHTTPUpgrader is a gobwas/ws HTTPUpgrader with default options. Use this in your HTTP handler *before* calling the Upgrade function below.

Functions

func Dial

func Dial(ctx context.Context, urlString string, opts types.TransportOptions) (types.Transport, error)

Dial establishes a WebSocket connection to the given URL string and returns a WebSocketTransport. It uses the gobwas/ws Dialer.

func Upgrade

func Upgrade(conn io.ReadWriter) (ws.Handshake, error)

Upgrade performs the WebSocket handshake on an existing network connection (io.ReadWriter). It's typically used on the server side after hijacking an HTTP connection. Returns the handshake information or an error. The original net.Conn should be used to create the WebSocketTransport if successful.

Types

type WebSocketTransport

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

WebSocketTransport implements the Transport interface using a gobwas/ws connection over net.Conn.

func NewWebSocketTransport

func NewWebSocketTransport(conn net.Conn, state ws.State, opts types.TransportOptions) *WebSocketTransport

NewWebSocketTransport creates a new WebSocketTransport wrapping an existing net.Conn. The state determines if outgoing frames should be masked (ws.StateClient).

func (*WebSocketTransport) Close

func (t *WebSocketTransport) Close() error

Close sends a close frame and closes the underlying WebSocket connection.

func (*WebSocketTransport) EstablishReceiver added in v0.1.11

func (t *WebSocketTransport) EstablishReceiver(ctx context.Context) error

EstablishReceiver for WebSocketTransport does nothing, as the connection is assumed to be established before the transport is created, and reading happens within the Receive method. It exists to satisfy the Transport interface.

func (*WebSocketTransport) IsClosed added in v0.1.11

func (t *WebSocketTransport) IsClosed() bool

IsClosed returns true if the transport connection is closed.

func (*WebSocketTransport) LocalAddr

func (t *WebSocketTransport) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*WebSocketTransport) Receive

func (t *WebSocketTransport) Receive(ctx context.Context) ([]byte, error)

Receive reads the next message from the WebSocket connection, respecting context. This replaces the old Receive and ReceiveWithContext methods. Uses read deadlines derived from the context for cancellation.

func (*WebSocketTransport) RemoteAddr

func (t *WebSocketTransport) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*WebSocketTransport) Send

func (t *WebSocketTransport) Send(ctx context.Context, data []byte) error

Send writes a message to the WebSocket connection as a TextMessage. Assumes data is a complete JSON message. Context is used for cancellation/deadlines.

type WebSocketTransportFactory

type WebSocketTransportFactory struct {
	Dialer         ws.Dialer   // gobwas Dialer
	Upgrader       ws.Upgrader // gobwas Upgrader (operates on io.ReadWriter)
	DefaultOptions types.TransportOptions
}

WebSocketTransportFactory can be used if a factory pattern is preferred.

func NewWebSocketTransportFactory

func NewWebSocketTransportFactory(opts types.TransportOptions) *WebSocketTransportFactory

NewWebSocketTransportFactory creates a factory with default dialer/upgrader.

func (*WebSocketTransportFactory) Dial

Dial uses the factory's dialer to establish a connection.

func (*WebSocketTransportFactory) Upgrade

func (f *WebSocketTransportFactory) Upgrade(conn net.Conn) (types.Transport, error)

Upgrade uses the factory's upgrader to perform the handshake on an existing connection. The caller must provide the underlying io.ReadWriter (e.g., from hijacking an HTTP request). Returns the *same connection* wrapped in a WebSocketTransport if successful.

Jump to

Keyboard shortcuts

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