43 lines
965 B
JavaScript
43 lines
965 B
JavaScript
var utils = require('./utils');
|
|
|
|
var WebSocketServer = require('ws').Server;
|
|
var wss = new WebSocketServer({port: 3434});
|
|
|
|
wss.broadcast = function(data, channel) {
|
|
if(!channel)
|
|
{
|
|
console.log('empty channel. skipped.');
|
|
return;
|
|
}
|
|
|
|
for(var i in this.clients) {
|
|
|
|
console.log('channel : ' + channel + 'client\'s channel : ' + this.clients[i].channel);
|
|
|
|
if(this.clients[i].channel == channel)
|
|
{
|
|
console.log('['+(new Date().toLocaleString())+'] ' + 'send : [' + channel +'] '+ data);
|
|
this.clients[i].send(data);
|
|
}
|
|
}
|
|
};
|
|
|
|
wss.on('connection', function(ws) {
|
|
ws.on('message', function(message) {
|
|
// console.log('['+(new Date().toLocaleString())+'] ' + 'received: %s\n', message);
|
|
|
|
if(typeof message == "string") {
|
|
// console.log('string message : ' + message);
|
|
|
|
var obj = JSON.parse(message);
|
|
// utils.ObjDump(obj);
|
|
if(obj.channel) {
|
|
ws.channel = obj.channel;
|
|
return;
|
|
}
|
|
}
|
|
|
|
wss.broadcast(message, ws.channel);
|
|
});
|
|
});
|