-
Notifications
You must be signed in to change notification settings - Fork 40
/
server.js
85 lines (62 loc) · 1.62 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
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
var http = require('http'),
app = require('express')(),
server = http.createServer(app),
io = require('socket.io').listen(server),
logger = require('winston'),
program = require('commander');
//
// SETUP
//
logger.cli();
logger.default.transports.console.timestamp = true;
program.version('0.1')
.option('-p, --port [num]', 'Set the server port (default 8080)')
.option('-H, --disableheartbeats', 'Disable heartbeats')
.parse(process.argv)
var server = "localhost";
if(program.args.length==1) {
server = program.args[0];
} else if (program.args.length==0) {
logger.warn("Defaulting to localhost.");
}
var port = 8080;
if(program.port) {
logger.info("Setting port to " + program.port);
port = program.port;
}
app.listen(port);
if(program.disableheartbeats) {
io.set("heartbeats", false)
}
io.set("log level", 0);
//
// LISTENERS
//
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
//
// GLOBALS
//
var connectedUsersCount = 0;
var messagesPerSecond = 0;
io.sockets.on('connection', function(socket) {
connectedUsersCount++;
socket.on('chat', function(data) {
// logger.info("chat message arrived");
io.sockets.emit('chat', {text:data.text});
messagesPerSecond++;
});
socket.on('disconnect', function(data) {
connectedUsersCount--;
});
});
setTimeout(logStatus, 1000);
//
// FUNCTIONS
//
function logStatus() {
setTimeout(logStatus, 1000);
logger.info("users: " + connectedUsersCount + "\tmessagesPerSecond: " + messagesPerSecond);
messagesPerSecond = 0;
}