This repository was archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (50 loc) · 1.61 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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cities = require('./cities');
var port = process.env.PORT || 3000;
// -----------------------------------------------------------------------------
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// -----------------------------------------------------------------------------
function warm(points){
points.forEach(function(point){
point.properties.diameter = 285000;
point.properties.scale = 0.05;
});
}
function selectPoints(){
var start = random(1,390);
var count = random(1,10);
var points = {
"type": "FeatureCollection"
};
points.features = cities.features.slice(start, start + count);
warm(points.features);
return points;
}
function send(socket){
var points = selectPoints();
socket.nbPoints += points.features.length;
socket.emit('points', points);
}
// -----------------------------------------------------------------------------
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
// -----------------------------------------------------------------------------
io.on('connection', function(socket){
socket.nbPoints = 0;
var emit = function(){
if(socket.nbPoints < 4000){
send(socket);
setTimeout(emit, 35)
}
}
emit();
});
// -----------------------------------------------------------------------------
http.listen(port, function() {
process.stdout.write('[Running] http://localhost:' + port);
});