Minimalist wrapper for `WebSocketServer` object from NPM `websocket` package.
npm install websocketserverhelperMinimalist wrapper for WebSocketServer object from NPM websocket package.
https://www.npmjs.com/package/websocket
Features :
- easy setup ;
- transparent event logging ;
- decouple connection handling from generic server logic.
This module is good for experimenting and quick prototyping due to its easy setup and rich log output.
However it might not be suited when precise configurations, performances, or security are required.
``javascript`
var wsServer = require('websocketserverhelper').createServer(
['echo'],
{
handleConnection: function(pConnection) {
pConnection.on('message', function(pMessageRaw) {
pConnection.send(pMessageRaw.utf8Data);
});
}
}
);
wsServer.start(36521);
Creates and returns a new Server object with the given connection handler.
protocols is the list of websocket sub-protocols supported by the server.acceptUndefinedProtocol
For every new connection the server selects the first sub-protocol requested by the client that is in this list.
If none of the protocols requested by the client is supported by the server,
or if no protocol is requested, no sub-protocol is selected.
In this case, if the property is set to false then the connection is rejected.supportedProtocols
Otherwise the connection is accepted with no sub-protocol.
This parameter defaults to an empty array if omitted, meaning that no protocol will ever be selected.
The list of supported protocols can be (re)set via the property.
connectionHandler must provide a function handleConnection which will be calledWebSocketConnection
for each connection the server receives, with the object passed as first parameter.connectionHandler
The handler can be (re)set via the property.
httpServer is the supportive HTTP server (native Node's http.Server object) used by the websocket server.httpServer
If omitted or invalid a default server is created.
The server can be accessed via the property.
Init a new WebSocket server.
See createServer() above.
If true, the server will be allowed not to select any sub-protocol, if it thinks it should do so based on the protocols requested by the client.false
If , any connection that make the server not select any sub-protocol will be rejected.
Default value is true.
The connection handler called each time a client connect to the server.
This property is set by the constructor. See constructor documentation for more infos.
This property can be (re)set at any time.
The supportive HTTP server used by the websocket server (native Node's http.Server object).
This property is set by the constructor. See constructor documentation for more infos.
Re(setting) this property has no effect.
The underlying native WebSocketServer object.
Array of websocket sub-protocols supported by the server.
This property is set by the constructor. See constructor documentation for more infos.
This property can be (re)set at any time.
Start the server on specified port.
If port is omitted a random port is selected.
WebSocket overview on MDN : https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
NPM websocket` package : https://www.npmjs.com/package/websocket
Alexandre Bintz
Comments and suggestions are welcome.