µ¿ÀÛ ¿Ϸá

This commit is contained in:
2016-02-09 20:08:31 +09:00
parent 04cda46062
commit 1f132c53a8
3 changed files with 36 additions and 105 deletions

View File

@@ -1,22 +1,21 @@
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;
// }
if(!channel)
{
console.log('empty channel. skipped.');
return;
}
for(var i in this.clients) {
// if(this.clients[i].channel == channel)
{
console.log('channel1 : ' + this.clients[i].channel);
console.log('channel2 : ' + channel);
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);
}
@@ -25,24 +24,17 @@ wss.broadcast = function(data, channel) {
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('['+(new Date().toLocaleString())+'] ' + 'received: %s\n', message);
try {
var obj = JSON.parse(message);
utils.ObjDump(obj);
if(obj.channel != null)
{
var channel = obj.channel;
// console.log('['+(new Date().toLocaleString())+'] ' + 'received: %s\n', message);
// if(wss.channel == null)
// wss.channel = new Object();
// if(wss.channel[channel] == null)
// wss.channel[channel] = new Array();
// wss.channel[channel].push(ws);
ws.channel = channel;
console.log('new channel : ' + ws.channel);
}
} catch (ex) {
console.log('['+(new Date().toLocaleString())+'] ' + ex);
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);