-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (41 loc) · 1.22 KB
/
server.js
File metadata and controls
44 lines (41 loc) · 1.22 KB
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
#!/usr/bin/node
var express = require('express');
var socket_io = require('socket.io');
var path = require('path');
/* Раздача статики */
var web = express();
web.set('PORT', process.env.PORT || 8080);
web.use(express.static(path.join(__dirname, 'public')));
var server = web.listen(web.get('PORT'), function () {
console.log('* Server start on http://localhost:' + web.get('PORT'));
});
/* Обработка запросов */
var io = socket_io.listen(server).on('connection', function(client) {
var send = function (message) {
message.from = client.id;
message.time = (new Date()).toISOString();
message.to = message.to || 'all';
message.type = message.type || 'message';
message.content = message.content || '';
var transit = function (otherClient) {
if (otherClient && client.id !== otherClient.id) {
otherClient.emit('message', message);
}
};
if (io.sockets) {
console.log('* Transit message', message);
if (message.to != 'all') {
transit(io.to(message.to));
} else {
for(var id in io.sockets) {
transit(io.sockets[id]);
}
}
}
};
send({type: 'connect'});
client.on('message', send);
client.on('disconnect', function () {
send({type: 'disconnect'});
});
});