A websocket server-client handlers framework based on Nodejs and Express
$ npm install
$ gulp
Websockets server uses following system environment variables for host and port configuration:
- WSS_HOST hostname or ip address to bind the server, default: 0.0.0.0;
- WSS_PORT port number to listen with, default: 4080;
- WSS_EXTURI external URI for client to connect, default: localhost:4080.
Example:
export WSS_HOST=0.0.0.0
export WSS_PORT=9080
export WSS_EXTURI=wsshost:9080
Messages are in JSON format, in the request must have a attribute "type", which is used to dispatch the request to handlers.
{
"type": "echo",
"msg": "hello websocket"
}
Request Handler is a function to client request, example:
function echoHandler(data) {
data.type = "echoback";
return data;
}
At the end of app.js after Websockets server started, you can register your handlers like:
wsrv.register("echo", echoHandler);
Note: You can register the handler type as "*", to handler all request.
Refer to public/js/wsclient.js and views/test_wss.pug for the example.
Websockets Server embed a websocket testing page, you can browse to http://WSS_HOST:WSS_PORT to use the page.