-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
41 lines (33 loc) · 1017 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
var WebSocketServer = require('ws').Server,
log4js = require('log4js'),
EventEmitter = require('events').EventEmitter,
config = require('./config'),
subscriber = new EventEmitter(),
uuid = require('uuid');
log4js.configure(config.log);
var log = log4js.getLogger();
exports.start = function (port) {
port = port || 8081;
var wss = new WebSocketServer({
port: port
});
wss.on('connection', function (ws) {
console.log('new connection');
ws.on('message', function (message) {
console.log('received: %s', message);
});
});
log.info('web socket server listening on port ' + port);
wss.broadcast = function (data) {
for (var i in this.clients)
this.clients[i].send(JSON.stringify(data));
};
// refact below
subscriber = new EventEmitter();
subscriber.on('message', function (data) {
wss.broadcast(data);
});
wss.subscriber = subscriber;
return wss;
};