Server

Server

new Server(wssOptions, serverOptionsopt, socketOptionsopt)

Source:

Starts a server.

Parameters:
Name Type Attributes Description
wssOptions Object

Options that are passed to ws server.

serverOptions Server.ServerOptions <optional>

Server options.

socketOptions Client.SocketOptions <optional>

Socket options.

Fires:
  • Server#event:ready
  • Server#event:error

Extends

  • EventEmitter

Methods

getClient(id) → {Client|undefined}

Source:

Git a client by id.

Parameters:
Name Type Description
id string

Client id.

Returns:

Client if found.

Type
Client | undefined

close(codeopt) → {Promise.<undefined>}

Source:

Closes a server.

Parameters:
Name Type Attributes Default Description
code code <optional>
1000

Code as per WebSocket spec.

Returns:

Promise.

Type
Promise.<undefined>

encodeMessage(event, …argsopt) → {Object}

Source:

Encode a message for a later use with Client#sendEncoded. Reserved event names (MUST NOT be used): connect, close, open, error, ping, pong, retry.

Parameters:
Name Type Attributes Description
event string

Event name.

args * <optional>
<repeatable>

Arguments.

Returns:

Encoded message.

Type
Object

encodeMessage(event, …argsopt) → {Object}

Source:

Encode a message for a later use with Client#sendEncoded. Reserved event names (MUST NOT be used): connect, close, open, error, ping, pong, retry.

Parameters:
Name Type Attributes Description
event string

Event name.

args * <optional>
<repeatable>

Arguments.

Returns:

Encoded message.

Type
Object

Type Definitions

ConnectionHook(client, auth) → {Promise.<(Object|undefined)>|Object|undefined}

Source:

Connection hook is run when a client connects to a server. The result is used as an auth reply data. May also return promises for an asynchronous execution. If the promise is rejected or an error is thrown, then auth has failed and the socket will be closed.

Parameters:
Name Type Description
client Client

Client.

auth Object

Auth data.

Returns:

Auth reply data.

Type
Promise.<(Object|undefined)> | Object | undefined

ServerOptions

Source:
Properties:
Name Type Attributes Default Description
connectionHook Server.ConnectionHook <optional>

Connection hook.

WebSocketServer Object <optional>

Alternative constructor for ws server.

authTimeout number <optional>
20000

Auth message wait timeout in ms.

Type:
  • Object