Documentation
¶
Index ¶
- Variables
- type CloseCode
- type CloseError
- type Config
- type Conn
- func (c *Conn) Close(code CloseCode, reason string) error
- func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error)
- func (c *Conn) ReadJSON(ctx context.Context, v any) error
- func (c *Conn) RemoteAddr() string
- func (c *Conn) Subprotocol() string
- func (c *Conn) Write(ctx context.Context, mt MessageType, data []byte) error
- func (c *Conn) WriteControl(opcode byte, payload []byte) error
- func (c *Conn) WriteJSON(ctx context.Context, v any) error
- type Hub
- type Logger
- type MessageType
- type NopLogger
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadRequest = errors.New("zsocket: bad websocket upgrade request") ErrHandshakeFailed = errors.New("zsocket: handshake failed") ErrOriginNotAllowed = errors.New("zsocket: origin not allowed") ErrSubprotocolRejected = errors.New("zsocket: subprotocol rejected") ErrClosed = errors.New("zsocket: connection closed") ErrTimeout = errors.New("zsocket: timeout") )
Functions ¶
This section is empty.
Types ¶
type CloseCode ¶
type CloseCode uint16
CloseCode follows RFC 6455 close codes (select common ones here).
const ( CloseNormalClosure CloseCode = 1000 CloseGoingAway CloseCode = 1001 CloseProtocolError CloseCode = 1002 CloseUnsupportedData CloseCode = 1003 CloseNoStatusRcvd CloseCode = 1005 // reserved - do not send CloseAbnormalClosure CloseCode = 1006 // reserved - do not send CloseInvalidFramePayloadData CloseCode = 1007 ClosePolicyViolation CloseCode = 1008 CloseMessageTooBig CloseCode = 1009 CloseMandatoryExtension CloseCode = 1010 CloseInternalServerErr CloseCode = 1011 )
type CloseError ¶
CloseError is returned when a close frame is received or needs to be sent.
func (CloseError) Error ¶
func (e CloseError) Error() string
type Config ¶
type Config struct {
// AllowedOrigins is used to check request Origin. Empty means "allow all".
AllowedOrigins []string
// Subprotocols contains allowed subprotocol names.
Subprotocols []string
// HandshakeTimeout controls the HTTP upgrade deadline.
HandshakeTimeout time.Duration
// ReadLimit caps the maximum message payload (bytes). 0 means no limit.
ReadLimit int64
// ReadBufferSize / WriteBufferSize are used for buffered I/O.
ReadBufferSize int
WriteBufferSize int
// PingInterval sets how often to send pings. 0 disables keepalive pings.
PingInterval time.Duration
// PongWait is how long to wait for a pong after a ping. Must be > 0 if PingInterval > 0.
PongWait time.Duration
// WriteTimeout caps write operations (frames & control frames).
WriteTimeout time.Duration
// CheckOrigin allows custom origin validation. If set, overrides AllowedOrigins.
CheckOrigin func(origin string) bool
// Logger is optional. If nil, logging is no-op.
Logger Logger
}
Config controls WebSocket server behavior during handshake and runtime.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a practical default config for servers.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a server-side WebSocket connection managed by zsocket. It exposes Read/Write methods, JSON helpers, and graceful Close. Concurrency: one reader goroutine internally; writes are serialized via writeMu.
NOTE: This MVP supports basic fragmentation (accumulate until FIN). Extensions are not implemented.
func Accept ¶
Accept upgrades an HTTP request to a WebSocket connection and returns a *Conn. It does a minimal RFC6455 handshake with origin & subprotocol checks, then hijacks the underlying TCP connection for frame I/O.
func (*Conn) Read ¶
Read reads the next complete message, returning its type and payload. It accumulates fragments until FIN=1. Control frames are handled internally.
func (*Conn) ReadJSON ¶
ReadJSON reads next message and unmarshals JSON into v. It accepts both text and binary frames. If binary, it still tries to decode JSON.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the peer address as string.
func (*Conn) Subprotocol ¶
Subprotocol returns the negotiated subprotocol, if any.
func (*Conn) Write ¶
Write sends a complete message (text or binary) as a single unfragmented frame.
func (*Conn) WriteControl ¶
WriteControl writes a control frame (ping/pong/close). Best-effort utility.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages subscriptions (rooms) and broadcasts messages to members. It is safe for concurrent use.
NOTE: Basic single-process hub. For horizontal scale, back it with Redis pub/sub or another broker.
type MessageType ¶
type MessageType byte
MessageType represents the WebSocket message type.
const ( TextMessage MessageType = 1 BinaryMessage MessageType = 2 )