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.
Summary
Properties
WebsocketServer.clock :: ClockWebsocketServer.handler :: Handler?WebsocketServer.onStop :: function?WebsocketServer.connections :: { Connection }WebsocketServer.server :: any?WebsocketServer.port :: numberWebsocketServer.url :: stringWebsocketServer.heartbeatIntervalMilliseconds :: numberWebsocketServer.connectAttempts :: numberWebsocketServer.refuseUpgrades :: numberWebsocketServer.upgradeDelaySeconds :: numberWebsocketServer.handshaking :: booleanWebsocketServer.pendingPaths :: { string }WebsocketServer.sequence :: numberWebsocketServer.sessionId :: string?WebsocketServer.dispatchLog :: { Payload }WebsocketServer.identifyCount :: numberWebsocketServer.handlerErrors :: { string }WebsocketServer.voiceServer :: VoiceServer?Methods
WebsocketServer:send(payload: Payload)WebsocketServer:close(code: number?)WebsocketServer:missDispatch(eventName: string, data: any)WebsocketServer:dispatch(eventName: string, data: any)WebsocketServer:moveVoice(guildId: string, channelId: string)WebsocketServer:disconnectVoice(guildId: string)WebsocketServer:latestConnection() → Connection?WebsocketServer:openConnections() → numberWebsocketServer:receivedOps(opcode: number) → { Received }WebsocketServer:stop()Properties
Section titled “Properties”The world clock: upgrade delays wait on it and received records are stamped with it.
handler
Section titled “handler”The test's handler, which answers every payload before the default replies.
onStop
Section titled “onStop”Called with the handle after it stops.
connections
Section titled “connections”Every connection ever opened, in open order; a closed one stays with open = false.
server
Section titled “server”The Zune server handle, nil after it stops.
The ephemeral port the server bound to.
The server's base URL, ws://127.0.0.1:{port}.
heartbeatIntervalMilliseconds
Section titled “heartbeatIntervalMilliseconds”The interval the default Hello announces, 41250 like Discord's.
connectAttempts
Section titled “connectAttempts”Every upgrade request the server has seen, refused ones included.
refuseUpgrades
Section titled “refuseUpgrades”How many of the next upgrades to refuse; each refusal fails the client with UpgradeFailed.
upgradeDelaySeconds
Section titled “upgradeDelaySeconds”Clock seconds each upgrade is held before it is answered, 0 for none.
handshaking
Section titled “handshaking”True while an upgrade is held by upgradeDelaySeconds.
pendingPaths
Section titled “pendingPaths”Paths of accepted upgrades whose connection has not opened yet, oldest first.
sequence
Section titled “sequence”The last dispatch sequence number the world handed out.
sessionId
Section titled “sessionId”The current session, stub-session-{identifyCount}, nil before the first Identify.
dispatchLog
Section titled “dispatchLog”The current session's dispatches other than READY and RESUMED, which a Resume replays.
identifyCount
Section titled “identifyCount”How many Identify payloads the default reply has answered.
handlerErrors
Section titled “handlerErrors”Every error the handler raised, which Zune would otherwise print and swallow.
voiceServer
Section titled “voiceServer”The voice server Voice State Update answers point at. Without one, Voice State Update gets no default reply.
Methods
Section titled “Methods”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? |
missDispatch
Section titled “missDispatch”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 |
dispatch
Section titled “dispatch”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 |
moveVoice
Section titled “moveVoice”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.
disconnectVoice
Section titled “disconnectVoice”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 |
latestConnection
Section titled “latestConnection”WebsocketServer : latestConnection ( ) -> Connection?
The most recently opened connection, nil before the first.
Returns
| Connection? |
openConnections
Section titled “openConnections”WebsocketServer : openConnections ( ) -> number
How many connections the server has not yet seen close.
Returns
| number |
receivedOps
Section titled “receivedOps”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.
Functions
Section titled “Functions”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 |