Documentation
¶
Overview ¶
Package websocket provides a types.Transport implementation using WebSockets.
Package websocket provides a types.Transport implementation using WebSockets.
Index ¶
- Variables
- func Dial(ctx context.Context, urlString string, opts types.TransportOptions) (types.Transport, error)
- func Upgrade(conn io.ReadWriter) (ws.Handshake, error)
- type WebSocketTransport
- func (t *WebSocketTransport) Close() error
- func (t *WebSocketTransport) LocalAddr() net.Addr
- func (t *WebSocketTransport) Receive() ([]byte, error)
- func (t *WebSocketTransport) ReceiveWithContext(ctx context.Context) ([]byte, error)
- func (t *WebSocketTransport) RemoteAddr() net.Addr
- func (t *WebSocketTransport) Send(data []byte) error
- type WebSocketTransportFactory
Constants ¶
This section is empty.
Variables ¶
var DefaultDialer = ws.Dialer{}
DefaultDialer is a gobwas/ws Dialer with default options.
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) LocalAddr ¶
func (t *WebSocketTransport) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*WebSocketTransport) Receive ¶
func (t *WebSocketTransport) Receive() ([]byte, error)
Receive reads the next message from the WebSocket connection.
func (*WebSocketTransport) ReceiveWithContext ¶
func (t *WebSocketTransport) ReceiveWithContext(ctx context.Context) ([]byte, error)
ReceiveWithContext reads the next message from the WebSocket connection with context support. Note: gobwas/ws doesn't directly support context cancellation for reads. We rely on read deadlines or closing the connection from another goroutine.
func (*WebSocketTransport) RemoteAddr ¶
func (t *WebSocketTransport) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*WebSocketTransport) Send ¶
func (t *WebSocketTransport) Send(data []byte) error
Send writes a message to the WebSocket connection as a TextMessage. Assumes data is a complete JSON message (including newline if needed by receiver).
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 ¶
func (f *WebSocketTransportFactory) Dial(ctx context.Context, urlString string) (types.Transport, error)
Dial uses the factory's dialer to establish a connection.
func (*WebSocketTransportFactory) Upgrade ¶
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.