-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (90 loc) · 3.46 KB
/
Copy pathapp.js
File metadata and controls
113 lines (90 loc) · 3.46 KB
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
var express = require('express')
, http = require('http')
, url = require('url')
, _ = require('underscore')
, path = require('path')
, io = require('socket.io')
, crypto = require('crypto');
var app = express();
var prompt = require('prompt');
var server = http.createServer(app);
var root = __dirname;
var io = require('socket.io').listen(server);
var socketCodes = {};
app.get('/', function(req, res){
res.status(200);
res.sendfile(root + '/public/splash.html');
});
app.configure(function(){
app.use(express.static(path.join(root, 'public')));
});
app.get('/:connectid', function(req, res){
res.status(200);
res.sendfile(root + '/public/index.html');
});
io.set('transports', ['websocket']);
io.sockets.on('connection', function(socket){
socket.emit('checkDevice',{});
socket.on('initiateDesktop', function(data){
var sessionTab = data.sessionTab;
var sessionWindow = data.sessionWindow;
var gameCode = crypto.randomBytes(3).toString('hex');
// Make sure the Token within the hash is unique
while(gameCode in socketCodes){
gameCode = crypto.randomBytes(3).toString('hex');
}
// Assign a key to the socket object and assign the value to the gameCode
socket.browserToken = gameCode;
socketCodes[socket.browserToken] = socket;
// Emit Gamecode to client to display to user
socket.emit("desktopAccessToken", gameCode);
}); // initiateDesktop connection
socket.on('initiateController', function(data){
var isValidSession = data.sessionHash;
// Cycle through all the desktop clients and find the browser with the same token as the mobile device
if (isValidSession in socketCodes){
// emit to Desktop Pair
socketCodes[isValidSession].emit("linkMobileDevice", {test:"We Are Connected to Phone"});
socket.emit("controllerAuthorization", isValidSession);
} else {
socket.emit("controllerAuthorization", false);
}
}); // initiateController connection
socket.on('swipe', function(data){
socketCodes[data.sessionHash].emit("swipe", {"direction": data.direction, "fingerCount" : data.fingerCount, "distance":data.distance, "duration":data.duration});
});
socket.on('move', function(data){
socketCodes[data.sessionHash].emit("move", {"dx": data.dx, "dy" : data.dy});
});
socket.on('pinchIn', function(data){
socketCodes[data.sessionHash].emit("pinchIn", {"direction": data.direction, "distance" : data.distance, "zoomScale" : data.zoomScale });
});
socket.on('pinchOut', function(data){
socketCodes[data.sessionHash].emit("pinchOut", {"direction": data.direction, "distance" : data.distance, "zoomScale" : data.zoomScale });
});
socket.on('pinchInTotal', function(data){
socketCodes[data.sessionHash].emit("pinchInTotal", {"zoomScale": data.zoomScale});
});
socket.on('pinchOutTotal', function(data){
socketCodes[data.sessionHash].emit("pinchOutTotal", {"zoomScale": data.zoomScale});
});
socket.on('zoomTapUndo', function(data){
var token = data.sessionHash;
if(token in socketCodes){
socketCodes[token].emit("zoomTapUndo", {});
}
});
socket.on('zoomTap', function(data){
socketCodes[data.sessionHash].emit("zoomTap", {});
});
socket.on('click', function(data){
socketCodes[data.sessionHash].emit("click", {});
});
}); // Main Connect
io.sockets.on('disconnect', function(socket){
if(socket.browserToken in socketCodes){
delete socketCodes[socket.browserToken];
}
});
//PRODUCTION_
server.listen(80);