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. |
Start listening for clients. Called automatically by the constructor unless autoStart is false.
Listen for an event.
name {String}The name of the event to listen to.callback {Function}The callback to execute when the event occurs.
Remove the listener for this event/callback.
| 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. |
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 the connection.
code {Number}Optional WebSocket close code.reason {String}Optional close reason.
Listen for an event.
name {String}The name of the event to listen to.callback {Function}The callback to execute when the event occurs.
Remove the listener for this event/callback.
| 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.