Skip to content

WebsocketServer

A localhost gateway: Hello on open, ACK on heartbeat, a new session on Identify, replay and RESUMED on Resume, the voice dispatches on Voice State Update once a voice server is attached, and whatever the handler returns. Every connection is dialled by URL, so the library's own shard code speaks to it unchanged, and every upgrade and payload is recorded, raw text included, for the test to assert on.

WebsocketServer.clock  :: Clock

The world clock: upgrade delays wait on it and received records are stamped with it.

WebsocketServer.handler  :: Handler?

The test's handler, which answers every payload before the default replies.

WebsocketServer.onStop  :: function?

Called with the handle after it stops.

WebsocketServer.connections  :: { Connection }

Every connection ever opened, in open order; a closed one stays with open = false.

WebsocketServer.server  :: any?

The Zune server handle, nil after it stops.

WebsocketServer.port  :: number

The ephemeral port the server bound to.

WebsocketServer.url  :: string

The server's base URL, ws://127.0.0.1:{port}.

WebsocketServer.heartbeatIntervalMilliseconds  :: number

The interval the default Hello announces, 41250 like Discord's.

WebsocketServer.connectAttempts  :: number

Every upgrade request the server has seen, refused ones included.

WebsocketServer.refuseUpgrades  :: number

How many of the next upgrades to refuse; each refusal fails the client with UpgradeFailed.

WebsocketServer.upgradeDelaySeconds  :: number

Clock seconds each upgrade is held before it is answered, 0 for none.

WebsocketServer.handshaking  :: boolean

True while an upgrade is held by upgradeDelaySeconds.

WebsocketServer.pendingPaths  :: { string }

Paths of accepted upgrades whose connection has not opened yet, oldest first.

WebsocketServer.sequence  :: number

The last dispatch sequence number the world handed out.

WebsocketServer.sessionId  :: string?

The current session, stub-session-{identifyCount}, nil before the first Identify.

WebsocketServer.dispatchLog  :: { Payload }

The current session's dispatches other than READY and RESUMED, which a Resume replays.

WebsocketServer.identifyCount  :: number

How many Identify payloads the default reply has answered.

WebsocketServer.handlerErrors  :: { string }

Every error the handler raised, which Zune would otherwise print and swallow.

WebsocketServer.voiceServer  :: VoiceServer?

The voice server Voice State Update answers point at. Without one, Voice State Update gets no default reply.

WebsocketServer : send ( payload )  -> ()

Sends one payload to every open connection.

Parameters

payload: Payload
WebsocketServer : close ( code )  -> ()

Closes every open connection with code (1000 by default).

Parameters

code: number?
WebsocketServer : missDispatch ( eventName ,  data )  -> ()

Logs a dispatch the client never received, under the next sequence number, so a later Resume replays it.

Parameters

eventName: string
data: any
WebsocketServer : dispatch ( eventName ,  data )  -> ()

Logs a dispatch as missDispatch does, then sends it to every open connection bound to the current session.

Parameters

eventName: string
data: any
WebsocketServer : moveVoice ( guildId ,  channelId )  -> ()

Dispatches the VOICE_STATE_UPDATE and VOICE_SERVER_UPDATE pair Discord sends when the bot moves to channelId in guildId. Errors when no voice server is attached.

Parameters

guildId: string
channelId: string
WebsocketServer : disconnectVoice ( guildId )  -> ()

Takes the stub user out of voice in guildId the way Discord does for a kick or a moderator disconnect: dispatches VOICE_STATE_UPDATE with a null channel, then closes every open voice connection with 4014. Errors when no voice server is attached.

Parameters

guildId: string
WebsocketServer : latestConnection ( )  -> Connection?

The most recently opened connection, nil before the first.

Returns

Connection?
WebsocketServer : openConnections ( )  -> number

How many connections the server has not yet seen close.

Returns

number
WebsocketServer : receivedOps ( opcode )  -> { Received }

Returns the records of every received payload with opcode across all connections, in the order they arrived.

Parameters

opcode: number

Returns

{ Received }
WebsocketServer : stop ( )  -> ()

Stops listening, closing every open connection first. Safe to call twice.

WebsocketServer . new ( settings )  -> WebsocketServer

Creates and starts the server on 127.0.0.1 with an ephemeral port.

Parameters

settings: {
clock: clock.Clock,
handler: Handler?,
voiceServer: voiceServer.VoiceServer?,
onStop: ((server,
}

Returns

WebsocketServer