Documentation
¶
Index ¶
- Constants
- Variables
- func Broadcast(message []byte, mType ...int)
- func EmitTo(uuid string, message []byte, mType ...int) error
- func EmitToList(uuids []string, message []byte, mType ...int)
- func Fire(event string, data []byte)
- func New(callback func(kws *Websocket), config ...websocket.Config) func(fiber.Ctx) error
- func On(event string, callback eventCallback)
- func Shutdown(ctx context.Context) error
- type AckCallback
- type EventPayload
- type Websocket
- func (kws *Websocket) Broadcast(message []byte, except bool, mType ...int)
- func (kws *Websocket) Close()
- func (kws *Websocket) Emit(message []byte, mType ...int)
- func (kws *Websocket) EmitArgs(event string, args ...[]byte)
- func (kws *Websocket) EmitEvent(event string, data []byte)
- func (kws *Websocket) EmitTo(uuid string, message []byte, mType ...int) error
- func (kws *Websocket) EmitToList(uuids []string, message []byte, mType ...int)
- func (kws *Websocket) EmitWithAck(event string, data []byte, cb func(ack []byte))
- func (kws *Websocket) EmitWithAckArgs(event string, args [][]byte, cb func([][]byte, error))
- func (kws *Websocket) EmitWithAckTimeout(event string, data []byte, timeout time.Duration, cb AckCallback)
- func (kws *Websocket) Fire(event string, data []byte)
- func (kws *Websocket) GetAttribute(key string) interface{}
- func (kws *Websocket) GetIntAttribute(key string) int
- func (kws *Websocket) GetStringAttribute(key string) string
- func (kws *Websocket) GetUUID() string
- func (kws *Websocket) HandshakeAuth() json.RawMessage
- func (kws *Websocket) IsAlive() bool
- func (kws *Websocket) IsPolling() bool
- func (kws *Websocket) SetAttribute(key string, attribute interface{})
- func (kws *Websocket) SetUUID(uuid string) error
Constants ¶
const ( // TextMessage denotes a text data message. The text message payload is // interpreted as UTF-8 encoded text data. TextMessage = 1 // BinaryMessage denotes a binary data message. BinaryMessage = 2 // CloseMessage denotes a close control message. The optional message // payload contains a numeric code and text. Use the FormatCloseMessage // function to format a close message payload. CloseMessage = 8 // PingMessage denotes a ping control message. The optional message payload // is UTF-8 encoded text. PingMessage = 9 // PongMessage denotes a pong control message. The optional message payload // is UTF-8 encoded text. PongMessage = 10 )
Source @url:https://github.com/gorilla/websocket/blob/master/conn.go#L61 The message types are defined in RFC 6455, section 11.8.
const ( // EventMessage is fired when a text or binary message is received that is // not bound to a named Socket.IO event (i.e. wire-level "message" events // or raw binary frames). EventMessage = "message" // EventPing is fired when a WebSocket PING control frame is received // from the peer. See // https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets EventPing = "ping" // EventPong is fired when a WebSocket PONG control frame, or an // Engine.IO PONG packet, is received from the peer. EventPong = "pong" // EventDisconnect is fired exactly once when the connection is torn // down, regardless of which side initiated the close. The // EventPayload.Error field carries the close reason (RFC 6455 section // 11.7) when available, or nil for a clean shutdown. EventDisconnect = "disconnect" // EventConnect is fired exactly once after the Engine.IO and Socket.IO // handshake completes, before the read loop starts dispatching events. // EventPayload.HandshakeAuth carries the client's auth payload, if any. EventConnect = "connect" // EventClose is fired exactly once when the connection is closed from // the server side via Websocket.Close. EventClose = "close" // EventError is fired when an error occurs on the connection (read, // write, parse, or listener panic). EventPayload.Error carries the // error value. EventError = "error" )
Supported event list
Variables ¶
var ( // ErrorInvalidConnection is returned when the addressed Conn connection is no // longer available; the error data carries the UUID of that connection. ErrorInvalidConnection = errors.New("message cannot be delivered invalid/gone connection") // ErrorUUIDDuplication is returned when the requested UUID already exists in // the active connections pool. ErrorUUIDDuplication = errors.New("UUID already exists in the available connections pool") // ErrAckNotRequested is returned by EventPayload.Ack when the event did // not include an ack id (i.e. the client emitted without a callback). ErrAckNotRequested = errors.New("socketio: event has no ack id") // ErrAckAlreadySent is returned by EventPayload.Ack when Ack() has // already been called once for this payload. ErrAckAlreadySent = errors.New("socketio: ack already sent") // ErrAckTimeout is delivered to outbound ack callbacks (registered via // EmitWithAckTimeout) when the client does not respond within the // configured timeout. ErrAckTimeout = errors.New("socketio: ack timeout") // ErrAckDisconnected is delivered to outbound ack callbacks when the // connection is torn down before an ack arrives. ErrAckDisconnected = errors.New("socketio: connection closed before ack") // ErrReservedEventName is surfaced via EventError when user code tries // to emit a name the JS socket.io client treats as a reserved // lifecycle event (e.g. "connect", "disconnect"). The emit is dropped // before the frame reaches the wire. ErrReservedEventName = errors.New("socketio: reserved event name cannot be emitted") // ErrAckIDOverflow is returned from inbound SIO frame parsing when the // numeric ack id prefix would overflow uint64. Surfaced via EventError. ErrAckIDOverflow = errors.New("socketio: ack id overflow") // ErrEmptyEventArray is returned from inbound SIO EVENT parsing when // the JSON-array payload is empty (no event name). Surfaced via // EventError. ErrEmptyEventArray = errors.New("socketio: empty event array") // ErrHandshakeClosed is returned from the EIO/SIO handshake when the // peer closes the connection before completing the SIO CONNECT step. ErrHandshakeClosed = errors.New("socketio: connection closed during handshake") // ErrInvalidNamespace is returned from the SIO CONNECT handshake when // the namespace prefix fails charset validation. The handshake is // rejected with CONNECT_ERROR before any user code runs. ErrInvalidNamespace = errors.New("socketio: invalid namespace in SIO CONNECT") // ErrInvalidAuthPayload is returned from the SIO CONNECT handshake // when the auth field is malformed JSON, exceeds MaxAuthPayload, or // is not a JSON object per socket.io-protocol v5. ErrInvalidAuthPayload = errors.New("socketio: invalid auth payload in SIO CONNECT") // ErrHeartbeatTimeout is the disconnection cause delivered to // EventDisconnect when no client PONG arrives within // PingInterval + PingTimeout. ErrHeartbeatTimeout = errors.New("socketio: heartbeat timeout") // ErrSendQueueClosed is the disconnection cause delivered to // EventDisconnect when the send queue is full and DropFramesOnOverflow // is false (the legacy hard-teardown path). Compare against // ErrSendQueueOverflow which fires on per-frame drops when // DropFramesOnOverflow is true. ErrSendQueueClosed = errors.New("socketio: send queue overflow") // ErrBatchPacketsExceeded is surfaced via EventError when a batched // EIO frame contains more than MaxBatchPackets record-separated // packets. Remaining packets in the frame are dropped. ErrBatchPacketsExceeded = errors.New("socketio: batched frame exceeds MaxBatchPackets") // ErrPollingBodyTooLarge is delivered to EventDisconnect when an // inbound polling POST exceeds PollingMaxBufferSize. The session // is torn down to mirror the WebSocket SetReadLimit behaviour; // repeated oversized POSTs would otherwise keep sessions alive // until heartbeat reaped them. ErrPollingBodyTooLarge = errors.New("socketio: polling POST body exceeds PollingMaxBufferSize") // ErrUnknownEIOPacket is surfaced via EventError when the inbound EIO // packet type byte does not match any recognised Engine.IO opcode. ErrUnknownEIOPacket = errors.New("socketio: unknown EIO packet type") )
var ( // PongTimeout is the legacy heartbeat timeout knob. // // Deprecated: PongTimeout is no longer consulted by the heartbeat // implementation. Use PingTimeout instead. PongTimeout = 20 * time.Second // RetrySendTimeout is the back-off delay the send goroutine waits // before retrying a failed write to a temporarily unavailable // connection. RetrySendTimeout = 20 * time.Millisecond // MaxSendRetry is the maximum number of times the send goroutine // retries a frame against a missing connection before dropping it. MaxSendRetry = 5 // ReadTimeout is no longer consulted by the read loop, which now // blocks in a single ReadMessage call gated by SetReadDeadline rather // than busy-polling with a sleep. // // Deprecated: kept only for backward compatibility with code that // still references the variable; setting it has no effect. ReadTimeout = 10 * time.Millisecond // PingInterval is how often the server sends Engine.IO PING packets // to the client. The client must reply with a PONG packet to keep // the connection alive. Read once per connection at handshake time; // mutating it does not affect already-open sockets. PingInterval = 25 * time.Second // PingTimeout is advertised to the client in the EIO OPEN packet and // is also used by the server-side heartbeat enforcer: a connection // is dropped when no frame arrives within PingInterval + PingTimeout. // Read once per connection at handshake time. PingTimeout = 20 * time.Second // HandshakeTimeout caps how long the server waits for the client SIO // CONNECT packet ("40") after sending the EIO OPEN packet. Set to // zero to disable. HandshakeTimeout = 10 * time.Second // MaxPayload is the maximum size in bytes of an inbound WebSocket // frame. It is advertised to the client in the EIO OPEN packet and // enforced via SetReadLimit on the underlying connection: frames // exceeding this size are rejected and the connection is closed. Set // to zero or negative to disable the limit (not recommended). MaxPayload int64 = 1_000_000 // MaxAuthPayload is the maximum size in bytes of the JSON auth // payload supplied by the client in the SIO CONNECT ("40") packet. // Per socket.io-protocol v5 the auth field is a JSON object (or // absent); any payload exceeding this cap is rejected with // CONNECT_ERROR and the handshake fails. Defaults to 8 KiB. Set to // zero to disable. MaxAuthPayload int = 8 * 1024 // OutboundAckTimeout is the default deadline for ack callbacks // registered via Websocket.EmitWithAck and Websocket.EmitWithAckArgs. // Use Websocket.EmitWithAckTimeout for per-call overrides. OutboundAckTimeout = 30 * time.Second // MaxBatchPackets caps the number of EIO packets accepted in a // single 0x1E-batched WebSocket frame. Without this cap, a frame // consisting almost entirely of record separators forces a // multi-megabyte slice header allocation. 256 is comfortably above // any legitimate batch. MaxBatchPackets = 256 // MaxEventNameLength bounds inbound SIO event name strings (in // bytes) so a hostile client cannot pin a multi-megabyte string per // frame inside the EventPayload dispatched to user listeners. Set // to zero to disable the bound (not recommended). MaxEventNameLength = 256 // SendQueueSize is the buffered capacity of the per-connection // outbound frame queue. Tune it before connections are accepted; // existing sockets retain the size in effect at New() time. SendQueueSize = 100 // DropFramesOnOverflow controls behavior when the per-connection // send queue is full. When false (default) the connection is torn // down with a "send queue overflow" error (legacy behavior); when // true the individual frame is dropped and EventError fires with // ErrSendQueueOverflow, allowing the connection to survive bursty // producers. DropFramesOnOverflow = false // ErrSendQueueOverflow is surfaced via EventError when a frame is // dropped because the send queue was full and DropFramesOnOverflow // is true. ErrSendQueueOverflow = errors.New("socketio: send queue overflow, frame dropped") )
Tunable package-level knobs. Mutate them before calling New so each new connection captures the desired value; in-flight connections retain the values in effect at handshake time and are not affected by later changes. These vars are not safe for concurrent mutation once connections are open.
var EnablePolling = false
EnablePolling toggles HTTP long-polling fallback support. When true, the handler returned from New() also accepts Engine.IO v4 polling requests (transport=polling) on the same route, in addition to the existing WebSocket upgrade. Default false preserves prior behavior.
Read once per request; mutate before serving connections.
To enable polling on the route, the user must mount the handler for both GET and POST methods, e.g.
h := socketio.New(cb)
app.Get("/socket.io/", h)
app.Post("/socket.io/", h)
or app.All("/socket.io/", h).
Polling sessions advertise an empty "upgrades" array in the Engine.IO OPEN packet: clients connected via polling stay on polling for the session lifetime. (Polling-to-WebSocket transport upgrade is not yet implemented; see issue tracker.)
var Logger func(level, msg string, fields ...any)
Logger is an optional package-level hook that, when non-nil, receives every internal warning/error the socketio package emits. The default (nil) preserves the historical "silent" behavior, so this is a non- breaking addition.
level is one of "warn" or "error". msg is a short, stable description of the event class (e.g. "handshake_failure", "queue_overflow", "ack_timeout"). fields is a flat key/value list of structured context suitable for forwarding to slog/zap/zerolog/etc., e.g.
Logger = func(level, msg string, fields ...any) {
slog.Default().Log(context.Background(), levelOf(level), msg, fields...)
}
Implementations MUST be safe for concurrent use and MUST NOT block; the hook is invoked from goroutines on the connection hot path.
Logger is read without synchronisation; assign it once during process startup before serving connections.
var MaxPollWait = 30 * time.Second
MaxPollWait caps how long a long-poll GET blocks waiting for outbound frames. The pong heartbeat (PingInterval, default 25s) typically enqueues a "2" PING packet within MaxPollWait, so most polls return well before this deadline. Set to zero to disable (not recommended).
var PollQueueMaxFrames = 1024
PollQueueMaxFrames caps the number of frames buffered in a polling session's outbound queue. When the cap is hit, behaviour mirrors the WebSocket SendQueueSize backpressure: with DropFramesOnOverflow=true the new frame is dropped and EventError fires with ErrSendQueueOverflow; otherwise the session is torn down with ErrSendQueueClosed. Without this cap, a slow or absent poller paired with a steady server-side emit stream would grow the queue without bound. Set to zero to disable the cap (not recommended).
var PollingMaxBufferSize = 1_000_000
PollingMaxBufferSize bounds the size of a single polling HTTP body (POST request body and GET long-poll response body) in bytes. Matches engine.io's maxHttpBufferSize default of 1 MB.
Functions ¶
func Broadcast ¶
Broadcast is the package-level form of Websocket.Broadcast. It sends message to every active connection in the pool, including the originator (use the method form to skip the originator). See Websocket.Emit for the meaning of mType.
func EmitTo ¶
EmitTo is the package-level form of Websocket.EmitTo. It sends message to the connection identified by uuid and returns ErrorInvalidConnection when the target is unknown or already closed.
func EmitToList ¶
EmitToList is the package-level form of Websocket.EmitToList. It sends message to every connection whose UUID appears in uuids. Errors are silently ignored; use the method form to receive them via EventError.
func Fire ¶
Fire delivers a synthetic event to the listeners registered for event on every active connection. It does not produce a wire frame. See Websocket.Fire for the per-connection variant.
func New ¶
New returns a Fiber handler that upgrades the request to a Socket.IO- compatible WebSocket, performs the Engine.IO / Socket.IO handshake, and invokes callback with the established Websocket so user code can register per-connection state before the read and heartbeat goroutines start.
Before delegating to the WebSocket upgrader, the handler validates the Engine.IO protocol version supplied via the "EIO" query parameter. Only EIO v4 is supported; an empty value defaults to v4. Any other value is rejected with HTTP 400 and a JSON error body matching the reference socket.io server response, so older clients (e.g. EIO v3) surface a recognisable, transport-level handshake error instead of being silently upgraded into an incompatible session.
func On ¶
func On(event string, callback eventCallback)
On registers callback for the named event. The callback fires for every connection that receives event, in registration order. Multiple callbacks may be registered for the same event; all run synchronously on the connection's read goroutine.
On is concurrency-safe and may be called at any time, including from inside another listener; later registrations may or may not be observed by concurrent dispatch loops (eventual consistency). Listeners cannot be unregistered. A listener that calls payload.Ack must do so synchronously or pass the payload to a goroutine that copies any byte slices it needs.
func Shutdown ¶ added in v1.2.0
Shutdown closes every active socket.io connection in the pool and waits for all of their per-connection goroutines (send, read, pong) to exit, or until ctx is cancelled.
Wire this into fiber.App.Shutdown / fiber.App.ShutdownWithContext so an application shutdown deterministically tears down sockets instead of relying on the framework to close the underlying transport.
Returns ctx.Err() when ctx is cancelled before all connections finished draining; otherwise returns nil.
Types ¶
type AckCallback ¶ added in v1.2.0
AckCallback receives the result of a server-initiated EmitWithAck.
On a successful ack, ack holds the raw JSON of the client's FIRST ack argument (or nil when the client acked with no arguments) and err is nil. Multi-argument acks are not surfaced through this single-arg shape; use EmitWithAckArgs when the client may ack with more than one argument or when "single arg that is itself a JSON array" must be distinguished from "multiple args".
On timeout err is ErrAckTimeout. On connection close err is ErrAckDisconnected. In both error cases ack is nil.
type EventPayload ¶
type EventPayload struct {
// Kws is the connection that fired the event. Use it to call
// Emit/EmitEvent/Close from inside the listener.
Kws *Websocket
// Name is the event name as registered with On (e.g. "message",
// EventConnect, or any custom event).
Name string
// SocketUUID is the unique identifier of the originating connection,
// captured at dispatch time so it remains stable even if the
// connection is concurrently closed.
SocketUUID string
// SocketAttributes is a defensive snapshot of the connection's
// attribute map taken at dispatch time. Mutating it does not affect
// the live connection; use Kws.SetAttribute for that.
SocketAttributes map[string]any
// Error is the cause associated with lifecycle events such as
// EventDisconnect and EventError; nil for ordinary user events.
Error error
// Data is the first event argument (if any). Kept for backwards
// compatibility; equivalent to Args[0] when len(Args) > 0, else nil.
Data []byte
// Args are the raw-JSON arguments the client sent with the event.
// Each entry is one JSON value; nil for events without args. Use
// this to consume socket.emit("event", a, b, c) from the JS client.
Args [][]byte
// AckID is the Socket.IO ack id attached to this event by the
// client. It is meaningful only when HasAck is true. Use Ack to
// respond.
AckID uint64
// HasAck reports whether the client requested an ack for this
// event (i.e. the JS side called socket.emit("event", data,
// callback)).
HasAck bool
// HandshakeAuth is the raw JSON auth payload the client supplied
// in its SIO CONNECT packet, copied for safety. nil if the client
// connected without an auth payload. Populated for EventConnect
// listeners; for other events use Kws.HandshakeAuth instead.
HandshakeAuth json.RawMessage
// contains filtered or unexported fields
}
EventPayload is the read-only value passed to every event listener. It carries the originating connection (Kws), the event arguments (Args, with Data as a shortcut to the first arg), ack bookkeeping (AckID, HasAck, Ack) and the handshake auth payload (HandshakeAuth, populated for EventConnect listeners only).
Byte-slice fields (Args, Data, HandshakeAuth) carry their own backing storage independent of the read buffer: parseSIOEvent copies inbound args, the read goroutine copies binary frames before dispatch, and HandshakeAuth is captured during the handshake. Listeners may safely retain these slices across goroutine boundaries.
func (*EventPayload) Ack ¶ added in v1.2.0
func (ep *EventPayload) Ack(args ...[]byte) error
Ack sends a Socket.IO ACK ("43") response back to the client for the event represented by this payload. The variadic args ...[]byte signature accepts zero arguments (empty ack), one argument (single value), or many arguments that are emitted as comma-separated values, mirroring the JS-side callback(a, b, c) shape. Valid JSON args are passed through; raw text args are encoded as JSON strings. Nil or empty entries are skipped.
Ack is idempotent across all listeners dispatched for the same inbound event: only the first invocation produces a wire frame, and subsequent calls (whether on the same payload or on a sibling payload handed to another listener) return ErrAckAlreadySent.
Returns an error if the event has no ack id, the connection is closed, or the ack has already been sent for this payload.
type Websocket ¶
type Websocket struct {
// Conn is the underlying Fiber WebSocket connection. Treat it as
// read-only from listener callbacks; writes must go through the
// Emit/EmitEvent/Broadcast methods so the send goroutine remains
// the sole writer.
Conn *websocket.Conn
// UUID is the unique identifier assigned to this connection and used as
// its key in the active connections pool.
UUID string
// Locals wraps the Fiber Locals lookup so listener callbacks can reach
// values stored on the originating request context.
Locals func(key string) interface{}
// Params wraps the Fiber Params lookup so listener callbacks can read
// route parameters from the originating request.
Params func(key string, defaultValue ...string) string
// Query wraps the Fiber Query lookup so listener callbacks can read
// query-string values from the originating request.
Query func(key string, defaultValue ...string) string
// Cookies wraps the Fiber Cookies lookup so listener callbacks can read
// cookie values from the originating request.
Cookies func(key string, defaultValue ...string) string
// contains filtered or unexported fields
}
Websocket represents a single Socket.IO connection on top of the underlying Fiber WebSocket. It carries the per-connection state (UUID, namespace, attributes, ack bookkeeping) and exposes the Emit/Broadcast/Close API that user code interacts with from inside listener callbacks.
func (*Websocket) Broadcast ¶
Broadcast sends message to every active connection in the pool. When except is true the originating connection is skipped. The optional mType selects the WebSocket frame type: omit it (or pass TextMessage) to wrap message as a Socket.IO "message" event; pass BinaryMessage to send the bytes verbatim as a binary frame.
func (*Websocket) Close ¶
func (kws *Websocket) Close()
Close actively closes the connection from the server side.
It is idempotent: the synchronous DISCONNECT plus close-frame write block runs at most once even when called concurrently. EventClose fires exactly once before the regular disconnected tear-down (which fires EventDisconnect). Callers may invoke Close from inside an event listener; it does not block on the listener's own goroutine.
Synchronous writes (bypassing kws.queue) are required so the frames reach the wire BEFORE disconnected() closes done and the send goroutine shuts down. The kws.mu write lock serialises with the send goroutine (which writes under kws.mu.RLock) so the Conn is never written concurrently.
closeOnce gates the actual write block so concurrent Close() callers do not double-write the disconnect frames and, more importantly, do not race the upgrade handler's deferred releaseConn() that nils the embedded *fasthttp.Conn the moment run() returns. The companion kws.mu.Lock()/Unlock() fence at the tail of run() guarantees an in-flight Close() write has completed before the handler returns and releaseConn() fires.
func (*Websocket) Emit ¶
Emit sends message to the client wrapped as a Socket.IO "message" event (use EmitEvent for named events). The message bytes may be valid JSON (object, array, string literal, number, etc.) or raw text; raw text is encoded as a JSON string so socket.io-client can parse the frame.
The optional mType selects the WebSocket frame type: omit it (or pass TextMessage) for a Socket.IO "message" event; pass BinaryMessage to send the bytes verbatim as a binary WebSocket frame. The connection's namespace (captured during the handshake) is mirrored on the wire.
Concurrency-safe: enqueues onto the per-connection send queue. Calls on already-disconnected sockets are a no-op. Behavior on a full queue is governed by DropFramesOnOverflow.
func (*Websocket) EmitArgs ¶ added in v1.2.0
EmitArgs sends a named socket.io event with multiple arguments, matching the JS-side call socket.emit("event", a, b, c). Valid JSON args are passed through; raw text args are encoded as JSON strings. Empty entries are skipped. The connection's namespace (captured during the handshake) is mirrored on the wire. Reserved event names are rejected as in EmitEvent. Concurrency-safe.
func (*Websocket) EmitEvent ¶ added in v1.2.0
EmitEvent sends a named socket.io event to the client. The data parameter may be valid JSON or raw text. The connection's namespace (captured during the handshake) is mirrored on the wire.
Reserved event names ("connect", "connect_error", "disconnect") are rejected: the call is dropped and EventError fires with ErrReservedEventName instead. Concurrency-safe.
func (*Websocket) EmitTo ¶
EmitTo sends message to the connection identified by uuid. Returns the underlying pool error (typically ErrorInvalidConnection) when the target is unknown or already closed; in either case an EventError is fired on kws so fan-out callers (EmitToList, Broadcast) do not have to re-fire it. See Websocket.Emit for the meaning of mType.
func (*Websocket) EmitToList ¶
EmitToList sends message to every connection whose UUID appears in uuids. Per-target failures are surfaced as EventError on kws by EmitTo (not returned). See Websocket.Emit for the meaning of mType.
func (*Websocket) EmitWithAck ¶ added in v1.2.0
EmitWithAck sends a named socket.io event and registers a callback that is invoked exactly once with the client's ack response. The connection's namespace (captured during the handshake) is mirrored on the wire.
The data parameter may be valid JSON or raw text. The callback receives the raw JSON bytes from the client's ack ([] for an empty ack, the first arg as JSON, or a JSON-array when the client called the ack with multiple args).
On a healthy round-trip the callback fires with the ack bytes. If the client does not respond within OutboundAckTimeout the callback fires with nil. If the connection closes before any ack arrives the callback fires with nil. Because both error paths surface as a nil ack, callers that need to distinguish "client never replied" from "connection torn down" should use EmitWithAckTimeout, which delivers ErrAckTimeout versus ErrAckDisconnected through its structured AckCallback.
func (*Websocket) EmitWithAckArgs ¶ added in v1.2.0
EmitWithAckArgs is the multi-arg + structured-error variant of EmitWithAck. It sends a named event carrying multiple arguments and registers a callback invoked exactly once when the client acks, on timeout (OutboundAckTimeout), or on connection close. The connection's namespace (captured during the handshake) is mirrored on the wire.
On success cb is called with (args, nil) where args is the slice of raw-JSON ack arguments the client sent. On timeout cb is called with (nil, ErrAckTimeout); on disconnect (nil, ErrAckDisconnected).
func (*Websocket) EmitWithAckTimeout ¶ added in v1.2.0
func (kws *Websocket) EmitWithAckTimeout(event string, data []byte, timeout time.Duration, cb AckCallback)
EmitWithAckTimeout is the timeout-aware variant of EmitWithAck. Pass timeout = 0 to disable the timeout (the callback only fires when the client acks or the connection closes). The connection's namespace (captured during the handshake) is mirrored on the wire.
The callback's err is one of: nil (ack received), ErrAckTimeout, or ErrAckDisconnected.
func (*Websocket) Fire ¶
Fire delivers a synthetic event to the listeners registered for event on this connection only. It does not produce a wire frame; use it to inject internal events from server-side code. The data slice is exposed as the listener's EventPayload.Data and as Args[0].
func (*Websocket) GetAttribute ¶
GetAttribute returns the per-connection attribute previously stored under key, or nil if no such attribute exists. Concurrency-safe.
func (*Websocket) GetIntAttribute ¶
GetIntAttribute returns the per-connection attribute under key as an int, or 0 if no such attribute exists. Panics if the stored value is not an int.
func (*Websocket) GetStringAttribute ¶
GetStringAttribute returns the per-connection attribute under key as a string, or "" if no such attribute exists. Panics if the stored value is not a string.
func (*Websocket) GetUUID ¶
GetUUID returns the unique identifier of this connection in a concurrency-safe manner.
func (*Websocket) HandshakeAuth ¶ added in v1.2.0
func (kws *Websocket) HandshakeAuth() json.RawMessage
HandshakeAuth returns the raw JSON auth payload supplied by the client in its SIO CONNECT packet (e.g. `{"token":"..."}`). It returns nil for clients that did not send an auth payload.
Typical use: validate the auth payload from inside the New() callback or the EventConnect listener, then call kws.Close() if invalid.
func (*Websocket) IsAlive ¶
IsAlive reports whether the connection is still considered active and able to deliver outbound frames. Lock-free.
func (*Websocket) IsPolling ¶ added in v1.2.0
IsPolling reports whether this session is bound to the HTTP long- polling transport rather than to a WebSocket. When true, kws.Conn is nil; user code that touches kws.Conn directly must guard with this check (or just use the transport-agnostic Emit / Broadcast / Ack / Close API, which works on both transports).
func (*Websocket) SetAttribute ¶
SetAttribute stores a per-connection key/value pair. Concurrency-safe. Listeners receive a defensive snapshot via EventPayload.SocketAttributes.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
auth-and-ack
command
Example: Socket.IO server with handshake auth, listener-side payload.Ack, server-initiated EmitWithAckTimeout, and graceful Shutdown(ctx) on SIGINT/SIGTERM.
|
Example: Socket.IO server with handshake auth, listener-side payload.Ack, server-initiated EmitWithAckTimeout, and graceful Shutdown(ctx) on SIGINT/SIGTERM. |