Skip to content

Instantly share code, notes, and snippets.

@kanreisa
Created March 30, 2011 09:52
Show Gist options
  • Save kanreisa/894150 to your computer and use it in GitHub Desktop.
Save kanreisa/894150 to your computer and use it in GitHub Desktop.
var http = require('http');
var fs = require('fs');
var sys = require('sys');
var io = require('socket.io');
var port = 13401;
var server = http.createServer(function(req, res){
res.writeHead(301, {
'Content-Type': 'text/html',
'Location': 'http://localhost/',
});
});
server.listen(port);
console.log('node.js/socket.io server listening on port %d', port);
/* set variables */
var json = {
'count' : 0,
'test' : 'hoge',
'chs' : {},
};
/* socket.io */
var socket = io.listen(server);
socket.on('connection', function(client){
json.count++;
client.broadcast(JSON.stringify(json));
client.send(JSON.stringify(json));
client.on('message', function(message){
var obj = JSON.parse(message);
switch(obj.method){
case 'create':
if((!obj.chName)||(!obj.opName)){
return;
}
json.chs[obj.chName] = {
'oparator' : obj.opName,
'state' : 'setup',
'src' : '',
'type' : '',
'time' : '',
'comment' : {
'name' : '',
'content' : '',
},
}
console.log('created channel ' + obj.chName + ' by ' + obj.opName);
break;
case 'config_source':
if((!obj.chName)||(!obj.src)||(!obj.type)){
return;
}
json.chs[obj.chName].state = 'stop';
json.chs[obj.chName].src = obj.src;
json.chs[obj.chName].type = obj.type;
break;
case 'config_state':
if((!obj.chName)||(!obj.state)){
return;
}
json.chs[obj.chName].state = obj.state;
break;
case 'config_time':
if((!obj.chName)||(!obj.time)){
return;
}
json.chs[obj.chName].time = obj.time;
break;
case 'chat_update':
if((!obj.chName)||(!obj.name)||(!obj.content)){
return;
}
json.chs[obj.chName].comment.name = obj.name;
json.chs[obj.chName].comment.content = obj.content;
console.log(obj.name + ' is said: ' + obj.content + ' on ' + obj.chName);
break;
}
client.broadcast(JSON.stringify(json));
client.send(JSON.stringify(json));
})
client.on('disconnect', function(){
json.count--;
client.broadcast(JSON.stringify(json));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment