Skip to content

Latest commit

 

History

History
81 lines (52 loc) · 4.34 KB

File metadata and controls

81 lines (52 loc) · 4.34 KB

Server

Constructor

The Server class takes the following arguments:

Parameter Type Default value Description
port Number 8080 Port to listen on.
host String '0.0.0.0' Host to listen on.
encoder BinaryEncoder | JsonEncoder new JsonEncoder() Encoder used to read/write event messages.
pingInterval Number 30 Ping frequency in seconds (0 to disable pings).
maxPayload Number 512 Packet max length in bytes (should be a power of two).
clients ClientDirectory new MapClientDirectory() Client directory.
autoStart Boolean true Whether the server should start listening immediately.

Methods

start()

Start listening for clients. Called automatically by the constructor unless autoStart is false.

on(name, callback)

Listen for an event.

  • name {String} The name of the event to listen to.
  • callback {Function} The callback to execute when the event occurs.

off(name, callback)

Remove the listener for this event/callback.

Events

Name Callback parameters Description
ready Server is listening and ready to accept connections.
client:join client Client New connected client.
client:leave client Client Client left.
error error Error An error occurred.

Client

Methods

send(name, data)

Send data to the other end of the WebSocket.

  • name {String} The name of the event (must be one from the list passed to the BinaryEncoder).
  • data {Number|String|Boolean|Object} Any data handled by the corresponding Codec.

close(code, reason)

Close the connection.

on(name, callback)

Listen for an event.

  • name {String} The name of the event to listen to.
  • callback {Function} The callback to execute when the event occurs.

off(name, callback)

Remove the listener for this event/callback.

Events

Name Callback parameters Description
open client Client Client connection is open and ready to transmit.
error error Error, client Client An error occurred.
close client Client Client connection is closed.
* eventData Number | String | Boolean | Object, client Client Every event sent through the websocket pipe will emit an event on the other end of the socket.

Note: open, error and close are reserved event names; the Encoder will throw an exception if you define a custom event with either of these names.