-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
215 lines (187 loc) · 7.88 KB
/
app.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Module dependencies.
*/
var express = require('express')
, conf = require('./conf')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, moment = require('moment')
, _ = require('underscore')._;
//required modules
require('express-namespace');
// configuration
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var env = process.env.NODE_ENV;
kProduction = env === 'production';
var server = conf.server[env];
//global mongoose
mongoose = require('mongoose');
Schema = mongoose.Schema;
ObjectId = mongoose.SchemaTypes.ObjectId;
User = mongoose.model('User', require('./models/user'));
Move = mongoose.model('Move', require('./models/move'));
Metric = mongoose.model('Metric', require('./models/metric'));
var db = mongoose.connect(conf.mongo.url[env]+conf.mongo.dbname[env], function(err) { if (err) { throw err; } });
var app = express();
app.configure(function(){
app.set('server', server);
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
require('./routes/users')(app);
require('./routes/test')(app);
server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
//global socket.io
io = require('socket.io').listen(server);
var RedisStore = require('socket.io/lib/stores/redis')
, redis = require('socket.io/node_modules/redis')
, pub = redis.createClient()
, sub = redis.createClient()
, client = redis.createClient();
io.set('store', new RedisStore({
redisPub : pub
, redisSub : sub
, redisClient : client
}));
//socket.io production settings - https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
io.sockets.on('connection', function (socket) {
var socketId = socket.id
console.log("server:got connection from " + socket.id);
socket.on('disconnect', function (data) {
console.log("server:got disconnect from " + socket.uname);
if(socket.role == 'admin') {
socket.leave('admin');
} else {
io.sockets.in('admin').emit('disconnected', {data: data, who: {uid: socket.uid, uname: socket.uname, uimg: socket.uimg}});
}
});
socket.on('init', function(data) {
console.log("server:got init " + data.uid + ' as ' + data.role);
socket.uid = data.uid;
socket.role = data.role;
if(socket.role == 'admin') {
console.log("server:admin has logged in!");
socket.join('admin');
socket.name = 'admin-'+socket.uid; //TODO - admin has socket.name, user socket.name is not set
socket.uname = 'admin';
socket.fname = 'admin';
socket.lname = 'admin';
socket.uimg = '';
} else {
console.log(socket.uid);
//we use the uid to determine if this is an existing user in our backend (based on the user id in the url), or an unknown visitor
//if we find the user, we will get the details from db and populate in user object, so the admin can identify the user in his web ui
//if we dont find the user, we will insert a new record for this anaonymous user with the uid passed
//TODO - a random uid should be generated when not present
User.findOne({"id": socket.uid}, function foundUser(err, user) {
if(!user) {
console.log("server:we have an unknown visitor");
// TODO - get user details from backend, to populate the user object
socket.name = 'user-' + socket.uid;
socket.uname = 'User-' + socket.uid;
socket.fname = 'Unknown';
socket.lname = 'Unknown';
socket.uimg = 'http://imga.nxjimg.com/emp_image/header/black/profile.gif';
//save user info to db
var user = new User();
user.id = socket.uid;
user.name = socket.uname;
user.firstname = socket.fname;
user.lastname = socket.lname;
user.image = socket.uimg;
user.save(function(err) {
if (!err) {
console.log("server:user saved");
console.log("server:new client to start transmitting...");
socket.emit('start', { message: 'you can start now' });
} else {
return console.log(err);
}
});
} else {
console.log("server:we have a known visitor with the info: " + user);
socket.name = 'user-' + socket.uid;
socket.uname = user.name;
socket.fname = user.firstname;
socket.lname = user.lastname;
socket.uimg = user.image;
console.log("server:new client to start transmitting...");
socket.emit('start', { message: 'you can start now' });
}
});
}
});
socket.on('move', function(data) {
// console.log("someone is on the move...");
if(socket.uname == undefined) {
console.log("ERROR:socket.uname is undefined --> uid",socket.uid)
} else {
console.log("server: " + socket.uname + " moved to " + data.x + ":" + data.y);
}
io.sockets.in('admin').emit('moved', {data: data, who: {uid: socket.uid, uname: socket.uname, uimg: socket.uimg}});
var move = new Move();
move.x = data.x;
move.y = data.y;
move.click = data.c;
move.sid = socket.id;
User.findOne({"id": socket.uid}, function foundUser(err, user) {
move._user = user._id;
move.uid = user.id;
// console.log("move" + move);
move.save(function(err) {
if (!err) {
// console.log("move saved");
//update metric
var metric = new Metric();
metric.x = (Math.round(data.x/10) * 10) + 5;
metric.y = (Math.round(data.y/10) * 10) + 5;
var count = data.c ? 10 : 1;
Metric.update({page: 'home', x: metric.x, y: metric.y}, {$inc: { 'count': count }}, {upsert: true}, function(err, metric_updated){
if(err) { throw err; }
// return console.log("move metric saved");
});
} else {
return console.log(err);
}
});
});
});
socket.on('getinsights', function(data) {
var metrics = Metric
.find({page: 'home'})
.sort('count', -1)
.exec(function(err, insights) {
// console.log("insights",insights[0].count);
// console.log(insights);
if(insights != undefined && insights[0] != undefined) {
socket.emit('sendinsights', {max: insights[0].count, data: insights});
}
});
});
});