-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·396 lines (354 loc) · 12.7 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, server = http.createServer(app)
, path = require('path')
, io = require('socket.io').listen(server);
app.configure(function(){
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(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.configure('production', function() {
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
// Code for Heroku socket.io compatibility
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
//io.set('close timeout', 30);
});
//
var rooms = [];
// Misc Calculation Functions
// Returns the distance between two latitudes and longitudes using the equirectangular and pythagorean approximation
// takes in decimal lats and longs, returns distance between them in meters
function calcDistSq(x1, y1, x2, y2) {
// http://www.movable-type.co.uk/scripts/latlong.html
// Equirectangular approximation and pythagorean theorem
x1 = x1 / 180 * Math.PI;
x2 = x2 / 180 * Math.PI;
y1 = y1 / 180 * Math.PI;
y2 = y2 / 180 * Math.PI;
var x = (y2 - y1) * Math.cos((x1 + x2) / 2);
var y = (x2 - x1);
return (x * x + y * y) * 40680159610000; // change to meters 40680159610000 = (6378.1 * 1000)^2
}
// Room class
function Room (id) {
this.id = id;
this.name = id;
this.desc = 'Default Public Room';
this.purpose = 'default public';
this.radiusSq = 4900; // default radiusSq to 70m
this.numUsers = 0;
this.latitude = 0;
this.longitude = 0;
this.sockets = [];
this.chats = [];
}
Room.prototype.addSocket = function (socket) {
if (socket.coords !== null && isNaN(socket.coords.latitude) === false && isNaN(socket.coords.longitude) === false) {
var plusMinus = 1;
this.latitude = (this.latitude * this.numUsers + socket.coords.latitude * plusMinus) / (this.numUsers + plusMinus);
this.longitude = (this.longitude * this.numUsers + socket.coords.longitude * plusMinus) / (this.numUsers + plusMinus);
}
this.numUsers++;
var hasSpace = false;
for (var i = 0; i < this.sockets.length; i++) {
if (this.sockets[i] == null) {
this.sockets[i] = socket;
hasSpace = true;
}
}
if (hasSpace === false) {
this.sockets[this.sockets.length] = socket;
}
console.log('Added socket to room ' + this.id);
//console.log(socket);
//console.log(this);
}
Room.prototype.removeSocket = function(socket) {
if (socket.coords !== null && isNaN(socket.coords.latitude) === false && isNaN(socket.coords.longitude) === false) {
var plusMinus = -1;
this.latitude = (this.latitude * this.numUsers + socket.coords.latitude * plusMinus) / (this.numUsers + plusMinus);
this.longitude = (this.longitude * this.numUsers + socket.coords.longitude * plusMinus) / (this.numUsers + plusMinus);
}
this.numUsers--;
var empty = true;
for (var i = 0; i < this.sockets.length; i++) {
if (this.sockets[i] !== null && this.sockets[i].clientId === socket.clientId) {
this.sockets[i] = null;
}
if (empty === true && this.sockets[i] !== null) {
empty = false;
}
}
if (empty === true) {
removeRoom(this.id);
}
console.log('Removed socket from room ' + this.id);
//console.log(socket);
//console.log(this);
}
// Might as well convert this to a binary search tree sometime..
function addRoom(roomId) {
var exists = false;
var nullVal = -1;
for (var i = 0; i < rooms.length; i++) {
if (rooms[i] != null && rooms[i].id === roomId) {
exists = true;
return rooms[i];
}
if (rooms[i] == null && nullVal === -1) {
nullVal = i;
}
}
var room = new Room(roomId);
if (exists === false) {
if (nullVal !== -1) {
rooms[nullVal] = room;
} else {
rooms[rooms.length] = room;
}
}
return room;
console.log('Added a new room ' + roomId);
//console.log(room);
}
function removeRoom(roomId) {
for (var i = 0; i < rooms.length; i++) {
if (rooms[i] !== null && rooms[i].id === roomId) {
console.log('ALERT! Room ' + rooms[i].id + ' has been set to NULL!');
rooms[i] = null;
}
}
console.log('Removed room ' + roomId);
}
// Calculates and returns the room closest to the given point.
// Creates new room if point is far away from other rooms
function findNearestRoomLoc(latitude, longitude) {
var closestDist = 1000000000, closestRoom = -1, nullVal = -1;
var distSq = 1000000000, room = null;
for (var i = 0; i < rooms.length; i++) {
room = rooms[i];
if (room == null) {
nullVal = i;
continue;
}
if (isNaN(room.latitude) === true || isNaN(room.longitude) === true) {
continue;
}
distSq = calcDistSq(latitude, longitude, room.latitude, room.longitude);
console.log('Calculating Distances:=============(' + latitude + ', ' + longitude + ') to (' + room.latitude + ', ' + room.longitude + ')============Distance Squared: ' + distSq);
if (distSq < room.radiusSq && room.purpose === 'default public') {
if (distSq < closestDist) {
closestDist = distSq;
closestRoom = i;
}
}
}
if (closestRoom !== -1) {
console.log('Found nearby room ' + rooms[closestRoom].id);
return rooms[closestRoom];
}
var room = new Room((new Date()).getTime());
if (nullVal !== -1) {
rooms[nullVal] = room;
} else {
rooms[rooms.length] = room;
}
console.log('Found no nearby room, so created one ' + room.id);
return room;
}
// Also creates a room if there is not one already
function getRoomFromId(roomId) {
var nullVal = -1;
for (var i = 0; i < rooms.length; i++) {
if (rooms[i] !== null && rooms[i].id == roomId) {
console.log('Found this room from the following id: ' + roomId);
//console.log(rooms[i]);
return rooms[i];
}
if (rooms[i] == null && nullVal === -1) {
nullVal = i;
}
}
console.log('==========No fitting room found!==========');
return null;
}
io.sockets.on('connection', function (socket) {
// Socket managerial functions
function changeRoom(newRoom) {
io.sockets.in(socket.room.id).emit('Delete user', socket.clientName, socket.clientId);
socket.room.removeSocket(socket);
socket.leave(socket.room.id);
socket.room = newRoom;
socket.join(newRoom.id);
socket.room.addSocket(socket);
socket.emit('Change room', newRoom.id, newRoom.name, getLobbyNames(), getLobbyIds());
for (var i = 0; i < socket.room.chats.length; i++) {
socket.emit('Display new chat', socket.room.chats[i].chatObj, socket.room.chats[i].senderName);
}
socket.broadcast.to(newRoom.id).emit('Display new nearby user', socket.clientName, socket.clientId);
}
// Emits a message to everyone!
function messageAll(message) {
io.sockets.in(socket.room.id).emit('announcement', message);
}
function getLobbyNames() {
var lobby = io.sockets.clients(socket.room.id);
var lobbyNames = [];
for (var i = 0; i < lobby.length; i++){
lobbyNames[lobbyNames.length] = lobby[i].clientName;
}
return lobbyNames;
};
function getLobbyIds() {
var lobby = io.sockets.clients(socket.room.id);
var lobbyIDs = [];
for (var i = 0; i < lobby.length; i++){
lobbyIDs[lobbyIDs.length] = lobby[i].clientId;
}
return lobbyIDs;
};
socket.on('Set client name and id', function (name, id) {
socket.clientName = name;
socket.clientId = id;
socket.emit('Update client name', socket.clientName);
socket.broadcast.to(socket.room.id).emit('Display new nearby user', socket.clientName, socket.clientId);
});
socket.on('Change client name', function (newName, oldName, clientId) {
socket.clientName = newName;
socket.emit('Update client name', newName);
io.sockets.in(socket.room.id).emit('Change nearby client name', newName, oldName, clientId);
});
socket.on('Change room name', function(newRoomName, clientName) {
socket.room.name = newRoomName;
io.sockets.in(socket.room.id).emit('Update room name', newRoomName, clientName)
});
// Change to returning rooms in distance from the socket position
socket.on('Get nearby rooms', function() {
// Stash names, IDs, and descriptions of rooms in separate arrays.
var roomNames = [];
var roomIds = [];
var roomDescs = [];
var roomCents = [];
for (var i = 0; i < rooms.length; i++){
if (rooms[i] != null) {
roomNames[roomNames.length] = rooms[i].name;
roomIds[roomIds.length] = rooms[i].id;
roomDescs[roomDescs.length] = rooms[i].desc;
roomCents[roomCents.length] = {
latitude: rooms[i].latitude,
longitude: rooms[i].longitude
};
}
}
// Send arrays to index.js for display in Bootstrap modal
socket.emit('Display nearby rooms', roomNames, roomIds, roomDescs, roomCents);
});
socket.on('Change room', function(newRoomId) {
if (socket.room.id !== newRoomId) {
var newRoom = getRoomFromId(newRoomId);
if (newRoom == null) {
changeRoom(addRoom(newRoomId));
} else {
changeRoom(newRoom);
}
}
});
socket.on('Create room', function(newRoomName, newRoomDesc){
var newRoom = addRoom(new Date().getTime());
newRoom.name = newRoomName;
newRoom.desc = newRoomDesc;
newRoom.purpose = 'private room';
changeRoom(newRoom);
});
socket.on('Get all lobby users', function () {
socket.emit('Display all lobby users', getLobbyNames(), getLobbyIds());
});
socket.on('Send new file', function (fpfile, senderName) {
io.sockets.in(socket.room.id).emit('Display new file', fpfile, senderName);
});
socket.on('Send new chat', function (inChatObj, inSenderName) {
io.sockets.in(socket.room.id).emit('Display new chat', inChatObj, inSenderName);
socket.room.chats[socket.room.chats.length] = {
senderName: inSenderName,
chatObj: inChatObj
};
});
// Runs on connection, with location info
socket.on('Use loc info', function(name, id, coords, ip) {
socket.coords = coords;
socket.clientName = name;
socket.clientId = id;
var room = findNearestRoomLoc(socket.coords.latitude, socket.coords.longitude);
socket.ip = ip;
socket.room = room;
console.log('Joining room ' + room.id);
socket.room.addSocket(socket);
socket.join(socket.room.id);
socket.emit('Initialize room', socket.clientName, socket.room.id, socket.room.name, getLobbyNames(), getLobbyIds(), false);
for (var i = 0; i < socket.room.chats.length; i++) {
socket.emit('Display new chat', socket.room.chats[i].chatObj, socket.room.chats[i].senderName);
}
//socket.emit('Join room', socket.room.name);
//socket.emit('Update client name', socket.clientName);
socket.broadcast.to(socket.room.id).emit('Display new nearby user', socket.clientName, socket.clientId);
//socket.emit('Display all lobby users', getLobbyNames(), getLobbyIds());
});
// Runs on connection, without location info
socket.on('Use ip info', function(name, id, ip) {
socket.coords = null;
socket.ip = ip;
socket.clientName = name;
socket.clientId = id;
socket.room = getRoomFromId(ip);
if (socket.room == null) {
socket.room = addRoom(ip);
}
socket.room.addSocket(socket);
socket.join(socket.room.id);
console.log('Joining room ' + ip);
socket.emit('Initialize room', socket.clientName, socket.room.id, socket.room.name, getLobbyNames(), getLobbyIds(), true);
for (var i = 0; i < socket.room.chats.length; i++) {
socket.emit('Display new chat', socket.room.chats[i].chatObj, socket.room.chats[i].senderName);
}
//socket.emit('Join room', socket.room.name);
//socket.emit('Update client name', socket.clientName);
socket.broadcast.to(socket.room.id).emit('Display new nearby user', socket.clientName, socket.clientId);
//socket.emit('Display all lobby users', getLobbyNames(), getLobbyIds());
});
socket.on('disconnect', function () {
if (socket.room != null){
socket.room.removeSocket(this);
console.log('Leaving room: ' + socket);
socket.broadcast.to(socket.room.id).emit('Delete user', socket.clientName, socket.clientId);
socket.leave(socket.room.id);
socket.room = null;
}
});
});
var port = process.env.PORT || app.get('port');
server.listen(port, function() {
console.log("Express server listening on port " + port + " in " + app.settings.env + " mode");
});