Skip to content

Latest commit

 

History

History
79 lines (49 loc) · 2.91 KB

API.md

File metadata and controls

79 lines (49 loc) · 2.91 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 to use to read/write event messages.
ping Number 30 Ping frequency in seconds (0 for no ping).
maxLength Number 512 Paquet max length in bit (should be a power of two).
protocols Array String[] ['websocket'] Protocols tu use

Methods

on(name, callback)

Listen for event.

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

off(name, callback)

Remove listener for this event / callback.

Note: If you're on node < 10.0.0, use removeListener method instead of off

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 occured.

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()

Close the connexion.

on(name, callback)

Listen for event.

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

off(name, callback)

Remove listener for this event / callback.

Note: If you're on node < 10.0.0, use removeListener method instead of off

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 and the Encoder will throw an exeption if you define a custom event with either of these names.