forked from mobl/mobl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeerserver.js
139 lines (132 loc) · 4.21 KB
/
peerserver.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* The server component
* Requires node.js (http://nodejs.org) and npm (http://npmjs.org)
* to install required libraries: npm install connect express socket.io
*/
var sys = require('sys');
var connect = require('connect');
var express = require('express');
var io = require('socket.io');
var repl = require('repl');
function log(o) {
sys.print(sys.inspect(o) + "\n");
}
var app = express.createServer(
connect.staticProvider('./samples/znake/www')
//connect.staticProvider('./www')
);
app.listen(8888);
var socket = io.listen(app);
servers = {};
socket.on('connection', function(client) {
client.on('message', function(msg) {
var message = JSON.parse(msg);
var type = message.type;
var messageId = message.id;
switch(type) {
case 'join':
if(servers[message.name]) {
client.id = Math.floor(Math.random() * 999999);
servers[message.name].clients[client.id] = client;
servers[message.name].host.send(JSON.stringify({
type: 'connect',
server: message.name,
client: client.id
}));
client.send(JSON.stringify({
type: 'join',
message: "ok"
}));
} else {
client.send(JSON.stringify({
type: 'join',
message: "not-exists"
}));
}
break;
case 'list':
client.send(JSON.stringify({type: 'list', message: Object.keys(servers)}));
break;
case 'create':
if(!servers[message.name]) {
var server = {
name: message.name,
clients: {},
host: client
};
servers[message.name] = server;
client.send(JSON.stringify({
type: 'create',
message: "ok"
}));
} else {
client.send(JSON.stringify({
type: 'create',
message: "already-exists"
}));
}
break;
case 'broadcast':
var msg = JSON.stringify({
type: 'broadcast',
server: message.server,
message: message.message
});
var clients = servers[message.server].clients;
for(var c in clients) {
if(clients.hasOwnProperty(c)) {
clients[c].send(msg);
}
}
break;
case 'reply':
var toclient = servers[message.server].clients[message.client];
toclient.send(JSON.stringify({
type: 'reply',
server: message.server,
client: message.client,
replyTo: message.replyTo,
message: message.message
}));
break;
case 'message':
servers[message.server].host.send(JSON.stringify({
type: 'message',
server: message.server,
client: client.id,
message: message.message
}));
break;
case 'query':
servers[message.server].host.send(JSON.stringify({
type: 'query',
server: message.server,
client: client.id,
id: messageId,
message: message.message
}));
break;
}
});
client.on('disconnect', function() {
for(var s in servers) {
if(servers.hasOwnProperty(s)) {
var server = servers[s];
delete server.clients[client.id];
server.host.send(JSON.stringify({type: 'disconnect', server: s, client: client.id}));
if(server.host === client) {
var clients = server.clients;
for(var c in clients) {
if(clients.hasOwnProperty(c)) {
clients[c].send(JSON.stringify({type: 'server-disconnect', server: s}));
}
}
log("Exit " + server.name);
delete servers[s];
}
}
}
});
});
console.log('Server running at http://127.0.0.1:8888/');
repl.start('server> ');