This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·104 lines (92 loc) · 2.85 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var DataSift = require('datasift-node');
var currentHash = false;
var clientCount = 0;
if (process.argv.length != 4) {
console.log('Usage: server.js <datasift_username> <datasift_api_key>');
process.exit(1);
}
var ds = new DataSift(process.argv[2], process.argv[3]);
var getNewHash = function(config, callback) {
var csdl = 'interaction.geo geo_box "' + config.bounds.ne[0] + ',' + config.bounds.sw[1] + ':' + config.bounds.sw[0] + ',' + config.bounds.ne[1] + '"';
if (config.csdl) {
csdl += ' and (' + config.csdl + ')';
}
if (config.sample && config.sample != 100) {
csdl += ' and interaction.sample <= ' + config.sample;
}
console.log('Validating CSDL: ' + csdl);
ds.validate({ 'csdl': csdl }, function (err, response) {
if (err) {
console.log(err);
io.emit('error', {'message': err.message});
} else {
console.log('Validated, compiling...');
ds.compile({ 'csdl': csdl }, function (err, response) {
if (err) {
console.log(err);
io.emit('error', {'message': err.message});
} else {
console.log('Hash: ' + response.hash);
callback(response.hash);
}
});
}
});
}
app.get('/', function(req, res){
res.sendFile(__dirname + '/www/index.html');
});
http.listen(3000, function(){
console.log('Listening on *:3000');
});
io.on('connection', function(socket) {
// Subscribe to the current hash on the first client's connection.
console.log('Client connected');
clientCount++;
if (currentHash && clientCount == 1) {
ds.subscribe(currentHash);
}
// Handle restart-stream messages.
socket.on('restart-stream', function(config){
if (currentHash) {
ds.unsubscribe(currentHash);
currentHash = false;
}
getNewHash(config, function(hash) {
currentHash = hash;
ds.subscribe(currentHash);
});
});
// Unsubscribe from the current hash on the last client's disconnection.
socket.on('disconnect', function() {
console.log('Client disconnected');
clientCount--;
if (currentHash && clientCount == 0) {
ds.unsubscribe(currentHash);
currentHash = '';
}
})
});
ds.on('connect', function () {
console.log('Connected to DataSift');
});
ds.on('disconnect', function() {
currentHash = false;
console.log('Disconnected. Reconnecting...');
setTimeout(function() { ds.connect(); }, 1000);
});
ds.on('error', function (error) {
console.log('DS error: ' + error);
io.emit('error', {'message': error.message});
});
ds.on('interaction', function (data) {
if (data.data.interaction.geo.latitude != 0 && data.data.interaction.geo.latitude != 1 && data.data.interaction.geo.latitude != -1 &&
data.data.interaction.geo.longitude != 0 && data.data.interaction.geo.longitude != 1 && data.data.interaction.geo.longitude != -1) {
io.emit('coords', data.data.interaction.geo);
}
});
ds.connect();