wsconnadapter

package
v0.27.0-rc.10 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 9 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedMessageType = errors.New("unexpected websocket message type")

ErrUnexpectedMessageType is returned by Read when the peer sends a frame that is not a binary one. The adapter carries a byte stream, so any other type is a protocol error rather than something to skip.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	UUID string

	Logger    *log.Entry
	CreatedAt time.Time
	// contains filtered or unexported fields
}

Adapter is a net.Conn backed by a websocket connection. The zero value is not usable; build one with New. Read and Write are each safe for concurrent use, Close is idempotent, and the keep-alive loop starts only once Ping is called.

func New

func New(conn *websocket.Conn, options ...Option) *Adapter

New wraps conn in an Adapter. The adapter takes ownership of conn: closing the adapter closes it, and the caller must not use conn directly afterwards. Keep-alive is off until Ping is called.

func (*Adapter) Close

func (a *Adapter) Close() error

Close stops the keep-alive loop and closes the underlying connection. It is idempotent — every call after the first returns the error the first one produced.

func (*Adapter) LocalAddr

func (a *Adapter) LocalAddr() net.Addr

LocalAddr returns the local address of the underlying websocket connection.

func (*Adapter) Ping added in v0.13.6

func (a *Adapter) Ping() chan bool

Ping starts the keep-alive loop and returns the channel each pong is announced on. It is idempotent: later calls return the same channel without starting a second loop. The channel is not buffered and a pong is dropped rather than blocking the read loop, so a caller that stops receiving slows nothing down.

A failed ping write is terminal — a broken pipe or a closed socket — so the adapter closes itself, which propagates teardown to whoever is reading. Missing pongs for pongTimeout has the same effect.

func (*Adapter) Read

func (a *Adapter) Read(b []byte) (int, error)

Read fills b from the current websocket frame and is safe for concurrent use — it holds a mutex because it advances the reader the next call resumes from.

A frame boundary is not the end of the stream: the adapter's semantics are a byte stream spread over many frames, so an io.EOF from the frame currently being read is reported as a nil error and the next call opens the following frame.

func (*Adapter) RemoteAddr

func (a *Adapter) RemoteAddr() net.Addr

RemoteAddr returns the peer address of the underlying websocket connection. Behind a proxy this is the proxy, not the device.

func (*Adapter) SetDeadline

func (a *Adapter) SetDeadline(t time.Time) error

SetDeadline applies t to both directions, and returns on the first failure — so a failure on the read side leaves the write deadline unchanged.

func (*Adapter) SetReadDeadline

func (a *Adapter) SetReadDeadline(t time.Time) error

SetReadDeadline applies t to the read side.

func (*Adapter) SetWriteDeadline

func (a *Adapter) SetWriteDeadline(t time.Time) error

SetWriteDeadline applies t to the write side. It takes the write mutex, so it waits for an in-flight Write rather than racing it.

func (*Adapter) Write

func (a *Adapter) Write(b []byte) (int, error)

Write sends b as a single binary frame and is safe for concurrent use. A short write is reported as it happened: the count returned is what the frame took.

type Option added in v0.22.0

type Option func(*Adapter)

Option configures an Adapter during New.

func WithDevice added in v0.22.0

func WithDevice(tenant string, device string) Option

WithDevice tags every log line the adapter emits with the tenant and device it belongs to, so a connection can be followed through the logs of a busy server.

func WithID added in v0.22.0

func WithID(id string) Option

WithID sets the adapter's UUID, which the caller uses to correlate the connection with its own bookkeeping. It is not read by the adapter itself.

Jump to

Keyboard shortcuts

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