-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
executable file
·53 lines (44 loc) · 1.71 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
var webSocketConnections = [];
var nextWebSocketConnectionID = 1;
initServer();
function initServer() {
var settings = getServerSettingsFromCommandLine();
createServer(settings);
}
function getServerSettingsFromCommandLine() {
var parseCommandLineArguments = require('./js/command-line-arguments');
var commandLineArguments = process.argv.slice(2);
return parseCommandLineArguments(commandLineArguments);
}
function createServer(settings) {
var createTwitterConnection = require('./js/twitter-connection');
var createWebSocketServer = require('./js/web-socket-server');
var twitterCredentials = require('./' + settings.twitterCredentialsFilename);
createTwitterConnection(twitterCredentials, settings.boundingBox, broadcastPayload);
createWebSocketServer(settings.port, serverName(), webSocketConnectionHandler);
}
function broadcastPayload(payload) {
var stringifiedPayload = JSON.stringify(payload);
webSocketConnections.forEach(function(webSocketConnection) {
webSocketConnection.send(stringifiedPayload);
});
}
function serverName() {
return 'Twitter Geo Server';
}
function webSocketConnectionHandler(webSocketConnection) {
addWebSocketConnection();
addWebSocketDisconnectionListener();
function addWebSocketConnection() {
webSocketConnection.id = nextWebSocketConnectionID;
webSocketConnections[webSocketConnection.id] = webSocketConnection;
nextWebSocketConnectionID += 1;
console.log('Client %d connected', webSocketConnection.id);
}
function addWebSocketDisconnectionListener() {
webSocketConnection.on('close', function() {
delete webSocketConnections[webSocketConnection.id];
console.log('Client %d disconnected', webSocketConnection.id);
});
}
}