websocketx

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package websocketx adapts a gorilla *websocket.Conn into an io.ReadWriteCloser so two terminal WebSocket legs can be spliced with a plain io.Copy pair.

gorilla forbids concurrent callers of its data-write methods (NextWriter / WriteMessage); calling them from two goroutines panics the connection. Every data and control/text write is therefore serialized through a single mutex. This is the load-bearing correctness property of the adapter: a byte pump and a future keepalive writer can safely share one connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

Conn wraps a gorilla WebSocket connection and exposes it as an io.ReadWriteCloser. Reads return message payloads and transparently skip zero-length keepalive frames; writes emit binary frames and are serialized so data and control writers never race.

func NewConn

func NewConn(conn *websocket.Conn) *Conn

NewConn wraps conn. The returned Conn owns all writes to conn; callers must not write to the underlying connection directly (doing so reintroduces the concurrent-writer panic this type exists to prevent).

func (*Conn) Read

func (c *Conn) Read(data []byte) (int, error)

Read returns the payload of the next data frame. Zero-length frames (used as cheap keepalives) are skipped so they never surface to io.Copy as a spurious empty read. A payload larger than the caller's buffer is retained and drained across subsequent reads.

func (*Conn) SetReadLimit

func (c *Conn) SetReadLimit(limit int64)

SetReadLimit caps the size of a single inbound message. An oversized frame makes the next Read return an error, which tears the bridge down. Passthrough to the underlying connection.

func (*Conn) SetWriteWait

func (c *Conn) SetWriteWait(d time.Duration)

SetWriteWait bounds every subsequent write with a per-write deadline. A write that cannot complete within d (e.g. a stalled peer or a half-open connection through a proxy) fails fast instead of blocking the byte pump indefinitely, which is what lets a wedged terminal leg tear down promptly rather than pinning the PTY drain. d<=0 disables the deadline. The deadline is set under the same mutex as the write itself, so it is always paired with its write and never races a concurrent writer.

func (*Conn) Write

func (c *Conn) Write(data []byte) (int, error)

Write sends data as a single binary WebSocket frame. Safe to call concurrently with WriteMessage; both serialize on the same mutex.

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data []byte) error

WriteMessage sends a single WebSocket frame of the given type, serialized against all other writers. Resize control frames are sent with websocket.TextMessage; this overrides the embedded method so the write mutex is always held.

Jump to

Keyboard shortcuts

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