Documentation
¶
Index ¶
- Variables
- type Adapter
- func (a *Adapter) Close() error
- func (a *Adapter) LocalAddr() net.Addr
- func (a *Adapter) Ping() chan bool
- func (a *Adapter) Read(b []byte) (int, error)
- func (a *Adapter) RemoteAddr() net.Addr
- func (a *Adapter) SetDeadline(t time.Time) error
- func (a *Adapter) SetReadDeadline(t time.Time) error
- func (a *Adapter) SetWriteDeadline(t time.Time) error
- func (a *Adapter) Write(b []byte) (int, error)
- type Option
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
LocalAddr returns the local address of the underlying websocket connection.
func (*Adapter) Ping ¶ added in v0.13.6
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 ¶
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 ¶
RemoteAddr returns the peer address of the underlying websocket connection. Behind a proxy this is the proxy, not the device.
func (*Adapter) SetDeadline ¶
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 ¶
SetReadDeadline applies t to the read side.
func (*Adapter) SetWriteDeadline ¶
SetWriteDeadline applies t to the write side. It takes the write mutex, so it waits for an in-flight Write rather than racing it.
type Option ¶ added in v0.22.0
type Option func(*Adapter)
Option configures an Adapter during New.
func WithDevice ¶ added in v0.22.0
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.