-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
60 lines (46 loc) · 1.47 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
var express = require( 'express' );
var app = express();
app.use( express.static( __dirname + '/public') );
var server = require( 'http' ).Server( app );
var io = require( 'socket.io' )( server );
// Store data
var stats = {
connections: 0,
touch: 0,
video: 0,
pages: {}
};
// Map of Socket.id to Socket object
var socketData = {};
// Namespace use when capturing data
var capture = io.of( '/capture' );
capture.on( 'connection', function( socket ) {
++stats.connections;
socket.on( 'client-data', function( data ) {
socketData[ socket.id ] = data;
stats.touch += ( data.touch? 1 : 0 );
stats.video += ( data.video? 1 : 0 );
var pageCount = stats.pages[ data.url ] || 0;
stats.pages[ data.url ] = ++pageCount;
console.log( stats );
dashboard.emit( 'stats-updated', stats );
} );
socket.on( 'disconnect', function() {
// Clear down stats for lost socket
--stats.connections;
stats.touch -= ( socketData[ socket.id ].touch? 1 : 0 );
stats.video -= ( socketData[ socket.id ].video? 1 : 0 );
--stats.pages[ socketData[ socket.id ].url ];
delete socketData[ socket.id ];
console.log( stats );
dashboard.emit( 'stats-updated', stats );
} );
} );
var dashboard = io.of( '/dashboard' );
dashboard.on( 'connection', function( socket ) {
// Send an update to the newly connected dashboard socket
socket.emit( 'stats-updated', stats );
} );
server.listen( 3000, function(){
console.log( 'listening on *:3000' );
} );