method-web-socket-expose
is a simple to use ws implementation.
npm install --save method-web-socket-expose
var ws = require('method-web-socket-expose');
var router = require('method-web-socket-expose').Router();
var server = new ws.start({host:"localhost", port: 8000});
router.setMethod('serverMethod', function (_return) {
_return(["What's Up!"], "Bye");
});
server.use(router);
var ws = require('method-web-socket-expose').client;
var client = ws.connect('ws://localhost:8000');
client.on('ready', function(){
client.serverMethod("tellmeSomething").then(function(res,err){
console.log(arguments);
});
});
var ws = require('method-web-socket-expose');
var server = new ws.start({host:"localhost", port: 8000});
server.on('connection', function(client){
setTimeout(function(){
client.execute('clientMethod',["hello", "Bye"]);
}, 3000);
});
var ws = require('method-web-socket-expose').client;
var client = ws.connect('ws://localhost:8000');
client.setMethod('clientMethod', function (response) {
console.log(response);
});
Start the web socket on http express server, in the file www
var app = require('../app');
var ws = require('method-web-socket-expose');
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Start server listening
**/
ws.getServer('my-ws-server').start(server,"my-ws-server");
.....
Using the ws server in app.js
var express = require('express');
var ws = require('method-web-socket-expose');
/**
* Create Server instance.
* *Without starting listen.
**/
var wsServer = ws.start({},'my-ws-server');
/*
* EXPOSE ROUTER METHOS TO WEB SOCKET
*/
wsServer.use( require('./routes/session') );
wsServer.use( require('./routes/employ') );
wsServer.use( require('./routes/person') );
wsServer.use( require('./routes/management') );
.....
var ws = require('method-web-socket-expose');
var server = new ws.start({host:"localhost", port: 8000});
server.on('connection', function(client){
client.execute('clientMethod',["hello","bye"]);
});
server.broadcast = function broadcast(data) {
server.clients.forEach(function each(client) {
client.send(data);
});
};