dreamworkAPI
Ctrl K
GitHub
classDocs / WebSocket

WebSocket

classsharedmenusource
Object → WebSocket

A WebSocket client. Backends are tried in order: gwsockets, tungstenite, then a WebClient / browser fallback on client and menu.

A socket stays alive while it is connecting, open or waiting for a reconnect, even without references to it, call close to release it.

Constructor

WebSocket(url: string | URL, options: WebSocket.Options?): WebSocket

Fields

NameTypeDescription
urlstringThe WebSocket URL. Read-only.
BackendName"tungstenite" | "gwsockets" | "webclient" | ""The loaded WebSocket backend, empty if none was found.
Capabilitiesdreamwork.engine.websocket.CapabilitiesFeatures supported by the loaded backend. Read-only.

Methods

attachAttaches a handler for the given event.
awaitWaits until the given event fires, then resumes with that event's arguments.
clearRemoves every handler for event, or every handler of every event when omitted.
closeCloses the connection and cancels a pending reconnect; a locally closed socket never reconnects.
detachDetaches a previously attached handler from the given event.
getStatusReturns the current connection status as a string.
isClosedReturns true when the socket is not connected, also while waiting for a reconnect.
isClosingReturns true while a graceful close is in progress.
isConnectedAlias of WebSocket:isOpen.
isConnectingReturns true while the handshake is in progress.
isOpenReturns true when the socket is open and ready to send / receive.
isReconnectingReturns true while a reconnect attempt is scheduled.
onceAttaches a one-shot handler for the given event.
openOpens the connection, cancelling a pending reconnect and resetting its attempt counter.
sendSends a message. Binary frames need Capabilities.binary, otherwise the payload is sent as text.
writeAlias of WebSocket:send.

#WebSocket:attach

sharedmenusource
function WebSocket:attach(event: WebSocket.Event, handler: function)

Attaches a handler for the given event.

Parameters
NameTypeDescription
eventWebSocket.EventThe event to listen to: "open", "message", "close", "error" or "reconnect".
handlerfunctionThe callback; signature depends on event (see overloads).
Overloads
function WebSocket:attach(self: WebSocket, event: "open", handler: WebSocket.OpenHandler)
function WebSocket:attach(self: WebSocket, event: "message", handler: WebSocket.MessageHandler)
function WebSocket:attach(self: WebSocket, event: "close", handler: WebSocket.CloseHandler)
function WebSocket:attach(self: WebSocket, event: "error", handler: WebSocket.ErrorHandler)
function WebSocket:attach(self: WebSocket, event: "reconnect", handler: WebSocket.ReconnectHandler)

#WebSocket:await

sharedmenuasyncsource
async function WebSocket:await(event: WebSocket.Event): any

Waits until the given event fires, then resumes with that event's arguments.

  • "open" returns at once when the socket is already open;
  • "close" returns the last close at once when the socket is closed and no reconnect is pending;
  • anything else throws when the socket is closed with no reconnect pending, or when the socket closes first (a failed reconnect attempt counts as a close).
Parameters
NameTypeDescription
eventWebSocket.EventThe event to wait for: "open", "message", "close", "error" or "reconnect".
Returns
TypeNameDescription
any...The arguments that event's handlers receive.
Overloads
function WebSocket:await(self: WebSocket, event: "open"): WebSocket
function WebSocket:await(self: WebSocket, event: "message"): WebSocket, string
function WebSocket:await(self: WebSocket, event: "close"): WebSocket, string | nil, integer | nil, boolean
function WebSocket:await(self: WebSocket, event: "error"): WebSocket, string
function WebSocket:await(self: WebSocket, event: "reconnect"): WebSocket, integer, number

#WebSocket:clear

sharedmenusource
function WebSocket:clear(event?: WebSocket.Event)

Removes every handler for event, or every handler of every event when omitted.

Parameters
NameTypeDescription
eventoptionalWebSocket.EventThe event to clear, or nil to clear all events.

#WebSocket:close

sharedmenusource
function WebSocket:close(force?: boolean)

Closes the connection and cancels a pending reconnect; a locally closed socket never reconnects.

With force, terminates immediately (code 1006) and discards queued outbound messages when possible; otherwise flushes first when the backend supports it, and terminates when the peer does not finish the close within close_timeout seconds.

Parameters
NameTypeDescription
forceoptionalbooleanImmediately terminate instead of a graceful close. Defaults to false.

#WebSocket:detach

sharedmenusource
function WebSocket:detach(event: WebSocket.Event, handler: function)

Detaches a previously attached handler from the given event.

Parameters
NameTypeDescription
eventWebSocket.EventThe event the handler was attached to.
handlerfunctionThe exact callback previously passed to attach or once.
Overloads
function WebSocket:detach(self: WebSocket, event: "open", handler: WebSocket.OpenHandler)
function WebSocket:detach(self: WebSocket, event: "message", handler: WebSocket.MessageHandler)
function WebSocket:detach(self: WebSocket, event: "close", handler: WebSocket.CloseHandler)
function WebSocket:detach(self: WebSocket, event: "error", handler: WebSocket.ErrorHandler)
function WebSocket:detach(self: WebSocket, event: "reconnect", handler: WebSocket.ReconnectHandler)

#WebSocket:getStatus

sharedmenunodiscardsource
function WebSocket:getStatus(): WebSocket.Status

Returns the current connection status as a string.

Returns
TypeNameDescription
WebSocket.Statusstatus"connecting", "open", "closing" or "closed".

#WebSocket:isClosed

sharedmenunodiscardsource
function WebSocket:isClosed(): boolean

Returns true when the socket is not connected, also while waiting for a reconnect.

Returns
TypeNameDescription
booleanis_closedtrue if status is "closed".

#WebSocket:isClosing

sharedmenunodiscardsource
function WebSocket:isClosing(): boolean

Returns true while a graceful close is in progress.

Returns
TypeNameDescription
booleanis_closingtrue if status is "closing".

#WebSocket:isConnected

sharedmenunodiscardsource
function WebSocket:isConnected(): boolean
Returns
TypeNameDescription
booleanis_connectedtrue if status is "open".

#WebSocket:isConnecting

sharedmenunodiscardsource
function WebSocket:isConnecting(): boolean

Returns true while the handshake is in progress.

Returns
TypeNameDescription
booleanis_connectingtrue if status is "connecting".

#WebSocket:isOpen

sharedmenunodiscardsource
function WebSocket:isOpen(): boolean

Returns true when the socket is open and ready to send / receive.

Returns
TypeNameDescription
booleanis_opentrue if status is "open".

#WebSocket:isReconnecting

sharedmenunodiscardsource
function WebSocket:isReconnecting(): boolean

Returns true while a reconnect attempt is scheduled.

Returns
TypeNameDescription
booleanis_reconnecting

#WebSocket:once

sharedmenusource
function WebSocket:once(event: WebSocket.Event, handler: function)

Attaches a one-shot handler for the given event.

The handler is detached automatically right before it runs.

Parameters
NameTypeDescription
eventWebSocket.EventThe event to listen to: "open", "message", "close", "error" or "reconnect".
handlerfunctionThe callback; signature depends on event (see overloads).
Overloads
function WebSocket:once(self: WebSocket, event: "open", handler: WebSocket.OpenHandler)
function WebSocket:once(self: WebSocket, event: "message", handler: WebSocket.MessageHandler)
function WebSocket:once(self: WebSocket, event: "close", handler: WebSocket.CloseHandler)
function WebSocket:once(self: WebSocket, event: "error", handler: WebSocket.ErrorHandler)
function WebSocket:once(self: WebSocket, event: "reconnect", handler: WebSocket.ReconnectHandler)

#WebSocket:open

sharedmenusource
function WebSocket:open()

Opens the connection, cancelling a pending reconnect and resetting its attempt counter.

Throws if the socket is already connecting, open or closing, or if the backend fails to create the socket.

#WebSocket:send

sharedmenusource
function WebSocket:send(message: string, is_binary?: boolean)

Sends a message. Binary frames need Capabilities.binary, otherwise the payload is sent as text.

Parameters
NameTypeDescription
messagestringThe payload to send.
is_binaryoptionalbooleanSend as a binary frame. Defaults to false.

#WebSocket:write

sharedmenusource
function WebSocket:write(message: string, is_binary?: boolean)

Alias of WebSocket:send.

Parameters
NameTypeDescription
messagestringThe payload to send.
is_binaryoptionalbooleanSend as a binary frame. Defaults to false.
Generated from the dreamwork meta branch.