Documentation
¶
Index ¶
Constants ¶
const ( // TextMessage denotes a UTF-8 encoded text WebSocket frame (opcode 1). TextMessage = 1 // BinaryMessage denotes a binary WebSocket frame (opcode 2). BinaryMessage = 2 )
WebSocket message type constants. These mirror the standard WebSocket frame types without importing gorilla/websocket, keeping this module free of external dependencies.
Variables ¶
var ( // ErrConnectionClosed is returned when sending to a connection that has already been closed. ErrConnectionClosed = errors.New("wspulse: connection is closed") // ErrSendBufferFull is returned when the outbound buffer is full. // The frame is dropped; handle backpressure at the application layer. ErrSendBufferFull = errors.New("wspulse: send buffer full, frame dropped") )
Sentinel errors shared across the wspulse ecosystem.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec interface {
// Encode serializes f into bytes ready to be sent as a WebSocket frame.
Encode(f Frame) ([]byte, error)
// Decode deserializes received WebSocket bytes into a Frame.
Decode(data []byte) (Frame, error)
// FrameType returns the WebSocket message type to use when sending:
// TextMessage (1) or BinaryMessage (2).
FrameType() int
}
Codec encodes and decodes Frames for transmission over a WebSocket connection.
var JSONCodec Codec = jsonCodec{}
JSONCodec is the default Codec. Frames are encoded as JSON text frames. Frame.Payload must be valid JSON bytes (e.g. the output of json.Marshal).
type Frame ¶
type Frame struct {
// Event identifies the frame purpose. wspulse does not interpret this value.
// Conventional values: "msg" (user data), "sys" (system event), "ack" (acknowledgement).
Event string
// Payload is the encoded message body. Its format is determined by the Codec.
Payload []byte
}
Frame is the minimal transport unit for all WebSocket communication. Payload bytes are opaque to the wspulse layer; their format depends on the Codec in use. When using JSONCodec (the default), Payload must be valid JSON bytes (e.g. output of json.Marshal). When using a binary codec, Payload is the codec-encoded bytes.
type Transport ¶ added in v0.3.0
type Transport interface {
// ReadMessage reads a complete message from the connection.
// The returned messageType is TextMessage (1) or BinaryMessage (2).
ReadMessage() (messageType int, p []byte, err error)
// WriteMessage writes a complete message to the connection.
// messageType should be TextMessage (1) or BinaryMessage (2),
// consistent with Codec.FrameType.
WriteMessage(messageType int, data []byte) error
// SetReadLimit sets the maximum size in bytes for a message read
// from the connection.
SetReadLimit(limit int64)
// SetReadDeadline sets the read deadline on the underlying
// network connection.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the write deadline on the underlying
// network connection.
SetWriteDeadline(t time.Time) error
// SetPongHandler sets the handler for pong messages received
// from the peer.
SetPongHandler(h func(appData string) error)
// Close closes the underlying network connection.
Close() error
}
Transport abstracts the WebSocket connection for testability. gorilla/websocket.Conn satisfies this interface via duck typing — no wrapper or adapter is needed in production code.