forked from rupl/screensizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (75 loc) · 2.11 KB
/
index.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
// Node/npm deps
var express = require('express');
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
var port = process.env.PORT || 3000;
var env = process.env.NODE_ENV || 'development';
var GA = process.env.GA || '';
var VISUALIZE_SELF = process.env.VISUALIZE_SELF || false;
// Initialize app
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
// Expose static assets
app.use(express.static(__dirname + '/public', {redirect: false}));
/**
* Main app URL. This page serves the visualization.
*/
app.get('/', function(req, res){
cons.dust('views/index.dust', {
GA: GA,
VISUALIZE_SELF: VISUALIZE_SELF
}, function (err, out) {
if (err) {console.error(err); }
res.send(out);
});
});
/**
* Listen for new visitors. We're using GET instead of sockets because it allows
* the visualizer to be standalone, and you don't have to require socket.io as a
* dependency on the site whose traffic is being tracked.
*/
app.get('/screen', function(req, res){
var props = req.query;
props = makeUnique(props);
// Broadcast
io.emit('add', props);
console.log('🔷➕ ', JSON.stringify(props));
res.sendStatus(200);
});
/**
* Someone connected.
*/
io.on('connection', function(socket){
// console.log('👥➕');
/**
* A new screen appears!
*/
socket.on('add', function(props){
props = makeUnique(props);
io.emit('add', props);
console.log('🔷 ', JSON.stringify(props));
});
/**
* Someone got bored.
*/
socket.on('disconnect', function(){
// console.log('👥💨 ');
});
});
/**
* Listen for users to connect
*/
http.listen(port, function(){
console.log('⚡ Listening on port ' + port);
});
/**
* Randomly generate a few properties of each screen.
*/
function makeUnique(props) {
// Give the screen a random HTML ID and color.
props.id = 'screen-' + Math.floor(Math.random() * 1000000000);
props.backgroundColor = 'rgb(' + Math.floor(Math.random() * 255) + ', ' + Math.floor(Math.random() * 255) + ', ' + Math.floor(Math.random() * 255) + ')';
// Send back the new object.
return props;
}