-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.js
90 lines (76 loc) · 2.46 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
var express =require('express');
var app = express.createServer();
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
var oneYear = 31557600000;
app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.errorHandler());
});
var io = require('socket.io').listen(app);
var rooms = [];
function room(roomSocket, roomId){
this.roomSocket = roomSocket;
this.roomId = roomId;
this.mobileSockets = [];
};
app.listen(8080);
io.sockets.on('connection', function (socket) {
socket.on("new room", function(data){
rooms.push(new room(socket, data.room));
});
socket.on("connect mobile", function(data, fn){
var desktopRoom = null;
for(var i = 0; i < rooms.length; i++){
if(rooms[i].roomId == data.room){
desktopRoom = i;
}
}
if(desktopRoom !== null){
rooms[desktopRoom].mobileSockets.push(socket);
socket.set('roomi', desktopRoom, function(){})
fn({registered: true});
rooms[socket.store.data.roomi].roomSocket.emit('add user', socket.id, data);
}else{
fn({registered: false, error: "No live desktop connection found"});
}
});
//Update the position
socket.on("update movement", function(data){
if(typeof socket.store.data.roomi !== 'undefined'){
if(typeof rooms[socket.store.data.roomi] !== 'undefined'){
rooms[socket.store.data.roomi].roomSocket.emit('update position', socket.id, data);
}
}
});
//When a user disconnects
socket.on("disconnect", function(){
console.log(rooms.length);
var destroyThis = null;
if(typeof socket.store.data.roomi == 'undefined'){
for(var i in rooms){
if(rooms[i].roomSocket.id == socket.id){
destroyThis = rooms[i];
}
}
if(destroyThis !== null){rooms.splice(destroyThis, 1);}
console.log(rooms.length);
}else{
var roomId = socket.store.data.roomi;
for(var i in rooms[roomId].mobileSockets){
if(rooms[roomId].mobileSockets[i] == socket){
destroyThis = i;
}
}
if(destroyThis !== null){rooms[roomId].mobileSockets.splice(destroyThis, 1);}
rooms[roomId].roomSocket.emit('remove user', socket.id);
}
});
});