1
0
mirror of https://github.com/balkian/Web4.0.git synced 2024-11-05 08:41:43 +00:00

Changed Client directive

This commit is contained in:
J.Fernando Sánchez 2012-04-22 05:22:01 +02:00
parent 8f56712380
commit 3cda73ebab
3 changed files with 51 additions and 15 deletions

View File

@ -3,7 +3,7 @@ io.sockets.on('connection', function (socket) {
console.log("New connection!"); console.log("New connection!");
socket.on('test', function (data,ack) { socket.on('test', function (data,ack) {
console.log(data); console.log(data);
var testObject = {"testKey" : "testValue", "testArray": ["value1", "value2", "value3"], "testDic": {"key1":"value1","key2":"value2"}, "testMix":["one",{"twoDic":["one","two"]}]}; var testObject = {"testKey" : "testValue", "testArray": ["value1", "value2", "value3"], "testDic": {"key1":"value1","key2":"value2"}, "testMix":["one",{"twoDic":[1,2,"2",2.0,2.1]}],"specialKey":"Ñoña-2_3"};
ack(testObject); ack(testObject);
console.log("I acked"); console.log("I acked");
}); });

View File

@ -13,6 +13,7 @@ html
}); });
socket.on('announcement', function (msg) { socket.on('announcement', function (msg) {
console.log("Received announcement");
$('#lines').append($('<p>').append($('<em>').text(msg))); $('#lines').append($('<p>').append($('<em>').text(msg)));
}); });
@ -24,7 +25,7 @@ html
}); });
socket.on('user message', function(data){ socket.on('user message', function(data){
message('cmd',data); message('>',data);
}); });
socket.on('execute', message); socket.on('execute', message);
socket.on('reconnect', function () { socket.on('reconnect', function () {
@ -59,18 +60,38 @@ html
$('#send-message').submit(function () { $('#send-message').submit(function () {
var msg = $('#message').val(); var msg = $('#message').val();
if(msg.charAt(0)!='/'){ if(msg.charAt(0)=='/'){
message('me', msg);
socket.emit('user message', msg);
}
else{
var i = msg.indexOf(' '); var i = msg.indexOf(' ');
var data = {}; var data = {};
data['name'] = msg.substring(1,i); data['name'] = msg.substring(1,i);
data['payload'] = msg.substring(i); var payload = msg.substring(i);
message('Exec:', msg); try{
socket.emit('command', data); payload = jQuery.parseJSON(payload);
}catch(err){
console.log("Not json");
} }
data['payload'] = payload;
message('Exec:', msg);
socket.emit('execute', data);
}
else if(msg.charAt(0)=='$'){
var i = msg.indexOf(' ');
var data = msg.substring(1);
console.log("Client:"+data);
message("Client:"+data);
try{
data = jQuery.parseJSON(data);
}catch(err){
console.log("Not json"+err);
}
console.log("Client(final):"+JSON.stringify(data));
socket.emit('client', data);
}
else{
message('me', msg);
socket.emit('user message', msg);
}
clear(); clear();
$('#lines').get(0).scrollTop = 10000000; $('#lines').get(0).scrollTop = 10000000;
return false; return false;

View File

@ -5,7 +5,7 @@ var Hook = require('hook.io').Hook,
app = express.createServer(), app = express.createServer(),
io = require('socket.io').listen(app); io = require('socket.io').listen(app);
app.configure(function () { app.configure(function () {
app.use(stylus.middleware({ src: __dirname + '/public', compile: compile })); app.use(stylus.middleware({ src: __dirname + '/public', compile: compile }));
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public'));
app.set('views', __dirname); app.set('views', __dirname);
@ -42,7 +42,7 @@ app.listen(3000, function () {
console.log('app listening on http://' + addr.address + ':' + addr.port); console.log('app listening on http://' + addr.address + ':' + addr.port);
}); });
monkey.connect(1337); monkey.listen(1337);
var nicknames = {}; var nicknames = {};
@ -51,11 +51,26 @@ io.sockets.on('connection', function (socket) {
socket.on('user message', function (data) { socket.on('user message', function (data) {
console.log(data); console.log(data);
}); });
socket.on('command', function (data,fn) { socket.on('execute', function (data,fn) {
var name = data['name']; var name = data['name'];
var payload = data['payload']; var payload = data['payload'];
console.log('Executing '+name+' with '+payload); console.log('Executing '+name+' with '+payload);
try{
monkey.emit(name,payload,fn); monkey.emit(name,payload,fn);
}catch(err){
console.log("Error:"+err);
}
});
socket.on('client', function(data,fn){
var id = data['id'];
var name = data['name'];
var payload = data['payload'];
console.log('Clienting '+name+'@'+id+' with '+payload);
try{
nicknames[id].emit(name,payload,fn);
}catch(err){
console.log("Error:"+err);
}
}); });
socket.on('nickname', function (nick, fn) { socket.on('nickname', function (nick, fn) {
if (nicknames[nick]) { if (nicknames[nick]) {