forked from mhevery/angular-node-socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (43 loc) · 1.1 KB
/
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
42
43
44
45
46
47
48
var http = require('http');
var io = require('socket.io').listen(8889); // for npm, otherwise use require('./path/to/socket.io')
var connect = require('connect');
var server = connect.createServer(
connect.favicon()
, connect.logger()
, connect.static(__dirname + '/public')
);
server.listen(8888);
function set(obj, path, value){
var lastObj = obj;
var property;
path.split('.').forEach(function(name){
if (name) {
lastObj = obj;
obj = obj[property=name];
if (!obj) {
lastObj[property] = obj = {};
}
}
});
lastObj[property] = value;
}
var model = {};
var clients = [];
// socket.io
io.sockets.on('connection', function(socket){
clients.push(socket);
// new client is here!
socket.on('channel', function(msg){
console.log('message:');
console.log(msg);
set(model, msg.path, msg.value);
clients.forEach(function(otherClient){
if (socket !== otherClient){
console.log("emitting..");
otherClient.emit("channel", msg);
}
});
console.log(msg);
});
socket.emit("channel", {path:'', value:model});
});