-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.js
228 lines (201 loc) · 8.05 KB
/
server.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
var express = require('express');
var api = require('./api');
var http = require('http');
var crypto = require('crypto');
// CONFIG
var port = process.env.PORT || 5000;
// WEB SERVER
var app = express();
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
app.use('/', express.static(__dirname + '/html'));
app.use('/v2', express.static(__dirname + '/html/index2.html'));
app.get('/api/token', api.getToken);
app.get('/api/uploadtoken', api.getUploadToken);
app.get('/api/sessionId', function(req, res) {
var sessionId;
do {
sessionId = randomValueBase64(6);
console.log("generated session id: " + sessionId);
}
while (sessionIds.indexOf(sessionId) > -1);
res.json(sessionId);
});
app.get('/join', function(req, res) {
var id = req.query.id;
res.redirect('/participant.html?session=' + id);
});
// Currently only return the URN - could also return
// the various explode, zoom factors, etc.
app.get('/api/getSession/:id', function(req, res) {
var sessionId = req.params.id;
var idx = sessionIds.indexOf(sessionId);
res.json(idx < 0 ? "" : models[idx]);
});
var server = http.createServer(app);
server.listen(port);
console.log('Listening on port ' + port + '...');
var sessionIds = [];
var models = [];
var zoomFactors = [];
var explodeFactors = [];
var isolateIds = [];
var hideIds = [];
var showIds = [];
var sectionPlanes = [];
var defZoom = null;
var defExplode = 0;
var defIsolate = [];
var defHide = [];
var defShow = [];
var defSection = [];
// WEB SOCKETS
var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log('a user connected (id=' + socket.id +')');
socket.on('create-session', function(session) {
console.log('session created (id=' + session.id +')');
// Add our session info to the beginning of our various arrays
sessionIds.unshift(session.id);
models.unshift(null);
zoomFactors.unshift(defZoom);
explodeFactors.unshift(defExplode);
isolateIds.unshift(defIsolate);
hideIds.unshift(defHide);
showIds.unshift(defShow);
sectionPlanes.unshift(defSection);
});
socket.on('join-session', function(session) {
console.log('user joined session (id=' + session.id +')');
var idx = sessionIds.indexOf(session.id);
if (idx > -1) {
// Add our user to the room for this session
socket.join(session.id);
if (models[idx]) {
// Bring this user up to speed with the state of the session
emitDirectAndLog(socket, { name: 'load', value: models[idx] });
if (zoomFactors[idx] !== defZoom) {
emitDirectAndLog(socket, { name: 'zoom', value: zoomFactors[idx] });
}
if (explodeFactors[idx] > defExplode) {
emitDirectAndLog(socket, { name: 'explode', value: explodeFactors[idx] });
}
if (isolateIds[idx] !== defIsolate) {
emitDirectAndLog(socket, { name: 'isolate', value: isolateIds[idx] });
}
if (hideIds[idx] !== defHide) {
emitDirectAndLog(socket, { name: 'hide', value: hideIds[idx] });
}
if (showIds[idx] !== defShow) {
emitDirectAndLog(socket, { name: 'show', value: showIds[idx] });
}
if (sectionPlanes[idx] !== defSection) {
emitDirectAndLog(socket, { name: 'section', value: sectionPlanes[idx] });
}
}
}
else {
console.log('could not find session (id=' + session.id +')');
emitDirectAndLog(socket, { name: 'load' });
}
});
socket.on('close-session', function(session) {
var idx = sessionIds.indexOf(session.id);
if (idx > -1) {
// Clear the model for participants
emitToGroupAndLog({ session: session.id, name: 'load', value: '' });
// Clean up state
sessionIds.splice(idx, 1);
models.splice(idx, 1);
zoomFactors.splice(idx, 1);
explodeFactors.splice(idx, 1);
isolateIds.splice(idx, 1);
hideIds.splice(idx, 1);
showIds.splice(idx, 1);
sectionPlanes.splice(idx, 1);
console.log('session closed (id=' + session.id +')');
}
});
socket.on('disconnect', function() {
console.log('a user disconnected (id=' + socket.id +')');
});
socket.on('lmv-command', function(command) {
var idx = sessionIds.indexOf(command.session);
if (idx > -1) {
if (command.name === 'load') {
// Create our default settings for the model
models[idx] = command.value;
zoomFactors[idx] = defZoom;
explodeFactors[idx] = defExplode;
isolateIds[idx] = defIsolate;
hideIds[idx] = defHide;
showIds[idx] = defShow;
sectionPlanes[idx] = defSection;
// Emit the load command
emitToGroupAndLog(command);
// Emit the defaults to the group participants (no need for hide/show)
emitToGroupAndLog({ session: command.session, name: 'zoom', value: defZoom });
emitToGroupAndLog({ session: command.session, name: 'explode', value: defExplode });
emitToGroupAndLog({ session: command.session, name: 'isolate', value: defIsolate });
emitToGroupAndLog({ session: command.session, name: 'section', value: defSection });
}
else {
if (command.name === 'zoom') {
zoomFactors[idx] = command.value;
}
else if (command.name === 'explode') {
explodeFactors[idx] = command.value;
}
else if (command.name === 'isolate') {
isolateIds[idx] = command.value;
if (command.value == defIsolate) {
hideIds[idx] = defHide;
showIds[idx] = defShow;
}
}
else if (command.name === 'hide') {
hideIds[idx] = hideIds[idx].concat(command.value);
showIds[idx] = stripIds(showIds[idx], command.value);
}
else if (command.name === 'show') {
showIds[idx] = showIds[idx].concat(command.value);
hideIds[idx] = stripIds(hideIds[idx], command.value);
}
else if (command.name === 'section') {
sectionPlanes[idx] = command.value;
}
emitToGroupAndLog(command);
}
}
else {
console.log('could not find session (id=' + command.session +')');
}
});
});
function stripIds(existing, ids) {
for (var i = 0; i < ids.length; i++) {
var idx = existing.indexOf(ids[i]);
if (idx > -1) {
existing.splice(idx, 1);
}
}
return existing;
}
function emitDirectAndLog(socket, command) {
socket.emit('lmv-command', command);
console.log(command);
}
function emitToGroupAndLog(command) {
io.to(command.session).emit('lmv-command', command);
console.log(command);
}
function randomValueBase64 (len) {
return crypto.randomBytes(Math.ceil(len * 3 / 4))
.toString('base64') // convert to base64 format
.slice(0, len) // return required number of characters
.replace(/\+/g, '0') // replace '+' with '0'
.replace(/\//g, '0'); // replace '/' with '0'
}