-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
157 lines (132 loc) · 3.98 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Main.js
*
* Initializes Flation application.
* Defines Socket.io events & handlers.
* Connects to Mongo as a data store.
* Writes logs to console & Loggly service.
*
*/
var http = require('http');
var static = require('node-static');
var file = new(static.Server)('.');
var Color = require('color'),
util = require('util'),
winston = require('winston'),
request = require('request');
// Winston logging setup.
var logger = new winston.Logger;
var options = {timestamp:true};
logger.add(winston.transports.Console, options);
var HOST = 'localhost';
var PORT = 1339;
// var apiUrl = v.API_HOST+':'+v.API_PORT;
var server = http.createServer(function(req,res) {
console.log('HTTP request',req.method,req.url);
req.addListener('end', function () {
// Serve files!
file.serve(req, res);
}).resume();
console.log('HTTP respone',res.statusCode);
}).listen(PORT);
logger.info('Running on port: '+ PORT );
// Object used for holding analytics data.
function Shlock(kind, method, url ) {
this.kind = kind;
this.method = method;
this.url = url;
this.time = new Date().toJSON();
};
function Circle(r, fill) {
this.r = r;
this.fill = fill;
};
// Each client will be assigned a random color and radius
// for the circles that they create.
// Returns a color object with randomly generated color;
function randomColor() {
var color = Color();
var temp = Math.floor(Math.random()*255);
color.red(temp);
temp = Math.floor(Math.random()*255);
color.green(temp);
temp = Math.floor(Math.random()*255);
color.blue(temp);
return color;
}
// Returns a random radius
function randomR() {
return Math.floor(Math.random()*20+4.5);
}
// Realtime communication with the browser via socket.io.
function getConnections(io) {
var data = {};
data.sockets = io.sockets.clients().length;
return data;
}
// Socket.io server connect event handler
function connectHandler(io,socket,source) {
socket.broadcast.emit('load',getConnections(io));
socket.emit('load',getConnections(io));
var s = new Shlock('socket.io','connect','unknown');
s.source = source;
logger.info(util.inspect(s));
}
// Socket.io server disconnect event handler
function disconnectHandler(io,source) {
io.sockets.emit('load',getConnections(io));
var s = new Shlock('socket.io','disconnect','unknown');
s.source = source;
logger.info(util.inspect(s));
}
var io = require('socket.io').listen(server, {log:false});
io.sockets.on('connection', function(socket) {
connectHandler(io,socket,'socket.io.server');
// Establishing random color for this client.
var color = randomColor();
socket.set('color',color.hexString(), function(color) {
logger.info(util.inspect(color));
});
// Establishing random node size for this client.
var r = randomR();
socket.set('r',r, function(r) {
logger.info(util.inspect(r));
});
var attrs = {};
attrs.color = color.hexString();
attrs.r = r;
socket.emit('nodeAttr',attrs);
logger.info(util.inspect(attrs));
socket.on('disconnect', function() {
setTimeout(disconnectHandler,200,io,'socket.io.client');
});
socket.on('client', function (data) {
data.source = 'socket.io.client';
logger.info(util.inspect(data));
});
socket.on('point', function(data) {
logger.info(util.inspect(data));
data.point = [data.coordX, data.coordY];
// Every point broadcast by a client will contain its
// random color and radius
socket.get('color',function(err,color) {
data.color = color;
});
socket.get('r',function(err,r) {
data.r = r;
});
// Sending point click event to all clients.
socket.broadcast.emit('point',data);
data.source = 'socket.io.client';
});
// Time to drive the replay visualization.
socket.on('replay', function(data) {
var throttle = 100,
length = 100;
// TODO: Pull fixed number of items from the DB
// Establish Timeout / Interval
});
});
io.sockets.on('disconnect', function(socket) {
disconnectHandler(io,socket,'socket.io.server');
});