forked from futurerealitylab/FutureClassroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
452 lines (419 loc) · 15.4 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
const fs = require("fs");
const http = require("http");
const https = require("https");
const WebSocket = require("ws");
const DataStore = require("./DataStore.js");
const util = require("util");
// const yargs = require('yargs');
// based on examples at https://www.npmjs.com/package/ws
const WebSocketServer = WebSocket.Server;
var env = "remote";
var port = 8447;
// process console parameters
var myArgs = process.argv.slice(2);
console.log('myArgs: ', myArgs);
myArgs.forEach((arg, index) => {
if (arg.includes("-")) {
var command = arg.substr(1);
switch (command) {
case 'env':
env = myArgs[index + 1];
console.log("update env with " + env)
break;
default:
break;
}
}
});
// Yes, TLS is required
const serverConfig =
env == "local"
? {
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem"),
}
: {
key: fs.readFileSync(
"/www/server/panel/vhost/cert/eye.3dvar.com/privkey.pem"
),
cert: fs.readFileSync(
"/www/server/panel/vhost/cert/eye.3dvar.com/fullchain.pem"
),
};
// ----------------------------------------------------------------------------------------
// Create a server for the client html page
const handleRequest = function (request, response) {
// Render the single client html file for any request the HTTP server receives
// console.log('request received: ' + request.url);
if (request.url.endsWith(".png")) {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
// console.log('request received: ' + request.url);
response.writeHead(200, { "Content-Type": "image/png" });
response.end(fs.readFileSync(request.url));
} else if (request.url.endsWith(".gif")) {
response.writeHead(200, { "Content-Type": "image/gif" });
response.end(fs.readFileSync(request.url));
} else if (request.url.endsWith(".fbx")) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.end(fs.readFileSync(request.url));
} else if (request.url.endsWith(".js")) {
response.writeHead(200, { "Content-Type": "application/javascript" });
if (request.url.includes("../")) {
response.end(fs.readFileSync(request.url.replace("../", "")));
} else {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
// console.log('request received: ' + request.url);
response.end(fs.readFileSync(request.url));
}
} else if (request.url.endsWith(".css")) {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
// console.log('request received: ' + request.url);
response.writeHead(200, { "Content-Type": "text/css" });
response.end(fs.readFileSync(request.url));
} else if (request.url.endsWith(".ogg")) {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
// console.log('request received: ' + request.url);
response.writeHead(200, { "Content-Type": "audio/ogg" });
response.end(fs.readFileSync(request.url));
} else if (request.url.endsWith(".svg")) {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
// console.log('request received: ' + request.url);
response.writeHead(200, { "Content-Type": "image/svg+xml" });
response.end(fs.readFileSync(request.url));
} else if (request.url.endsWith(".html")) {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
// console.log('request received: ' + request.url);
response.writeHead(200, { "Content-Type": "text/html" });
response.end(fs.readFileSync(request.url));
} else if (request.url == "/") {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(fs.readFileSync("./index.html"));
} else {
if (!request.url.startsWith(".")) {
request.url = "." + request.url;
}
response.writeHead(200, { "Content-Type": "text/plain" });
response.end(fs.readFileSync(request.url));
}
};
var HTTPS_PORT = port; //default port for https is 443
var HTTP_PORT = HTTPS_PORT - 442; //default port for http is 80
const httpsServer = https.createServer(serverConfig, handleRequest);
httpsServer.listen(HTTPS_PORT);
// console.log("httpsServer:", httpsServer);
// ----------------------------------------------------------------------------------------
// Create a server for handling websocket calls
const wss =
// argv.env == "local" ? new WebSocketServer({ port: 3448 }) :
new WebSocketServer({ server: httpsServer });
// console.log("wss:" + wss.options.host + "-" + wss.options.path + ":" + wss.options.port);
let wsIndex = 100;
let websocketMap = new Map();
const datastore = new DataStore();
let avatars = {};
let timers = {};
const AVATAR_RATE = 10;
const OBJECT_RATE = 10;
let objectIndex = 1000;
setInterval(() => {
console.log("current connections:");
console.log(Array.from(websocketMap.keys()));
// console.log("avatars: ");
// console.log(avatars);
// for (let id in avatars) {
// var quat = avatars[id]["state"]["rot"];
// var fwd = [0, 0, 0];
// transformQuat(fwd, [0, 0, -1], quat);
// console.log("id", id, "pos:", JSON.stringify(avatars[id]["state"]["pos"]), "fwd", JSON.stringify(fwd));
// }
}, 5000);
function transformQuat(out, vec, quat) {
var q = quat;
if (quat.x != undefined) q = [quat.x, quat.y, quat.z, quat.w];
var a = vec;
if (vec.x != undefined) a = [vec.x, vec.y, vec.z];
let qx = q[0],
qy = q[1],
qz = q[2],
qw = q[3];
let x = a[0],
y = a[1],
z = a[2];
let uvx = qy * z - qz * y,
uvy = qz * x - qx * z,
uvz = qx * y - qy * x;
let uuvx = qy * uvz - qz * uvy,
uuvy = qz * uvx - qx * uvz,
uuvz = qx * uvy - qy * uvx;
let w2 = qw * 2;
uvx *= w2;
uvy *= w2;
uvz *= w2;
uuvx *= 2;
uuvy *= 2;
uuvz *= 2;
out[0] = x + uvx + uuvx;
out[1] = y + uvy + uuvy;
out[2] = z + uvz + uuvz;
return out;
}
function send(to, from, message) {
if (to == "*") {
// send to all
wss.clients.forEach(function each(client) {
// console.log("send ", client.index, client.isAlive, client.readyState);
if (from == client.index) {
return;
}
if (client.readyState === WebSocket.OPEN) {
// console.log('\tsend to', client.index, 'message:' + util.inspect(message["type"], {showHidden: false, depth: null}));//util.inspect(entry["message"], {showHidden: false, depth: null})
client.send(JSON.stringify(message));
} else if (client.readyState === WebSocket.CLOSING) {
console.log("ws not open:", client.index, message);
} else if (client.readyState === WebSocket.CLOSED) {
console.log("ws not open:", client.index, message);
} else if (client.readyState === WebSocket.CONNECTING) {
console.log("ws not open:", client.index, message);
}
});
} else {
console.log("sending to:", to);
const dst = websocketMap.get(to);
if (dst) {
dst.send(JSON.stringify(message));
}
}
}
function leave(index, username) {
console.log("close: websocketMap.keys():", Array.from(websocketMap.keys()));
if (!websocketMap.get(index)) {
return;
}
delete avatars[index];
console.log(avatars);
// clearInterval(timerID);
// TODO: change ip to username
console.log(index);
const response = { type: "leave", user: index };
send("*", index, response);
websocketMap.get(index).close();
websocketMap.delete(index);
}
wss.on("connection", function (ws, req) {
ws.index = wsIndex++;
websocketMap.set(ws.index, ws);
console.log("connection:", req.connection.remoteAddress, ws.index);
console.log('datastore.state["objects"]', datastore.state["objects"]);
const payload2others = { type: "initialize", id: ws.index, avatars: avatars };
const payload2self = {
type: "object",
data: {
len: Object.keys(datastore.state["objects"]).length,
ds: datastore.state["objects"],
},
ts: Date.now(),
};
send(ws.index, -1, payload2others);
// send('*', ws.index, payload2self);
// notify the world that a player joined, should be a separate process from initialize
// TODO: change id to username or something
send("*", -1, { type: "join", id: ws.index });
ws.on("message", function (data) {
// Broadcast any received message to all clients
//wss.broadcast(message);
// deal with it according to different msg
// console.log(data);
let json = {};
try {
json = JSON.parse(data.toString());
} catch (err) {
// console.log(err);
return;
}
switch (json["type"]) {
// ZH: obj
case "object": {
// about manipulation
console.log("receive ws msg:", json["type"]);
const key = json["uid"];
const id = json["id"];
const state = json["state"];
datastore.setObjectData(id, state);
break;
}
// ZH: obj init
case "objectInit": {
const key = json["uid"];
const state = json["state"];
console.log("receive ws msg:", json["type"]);
if (state["objid"] == 0) {
// create by user
console.log("create object from user", json["uid"]);
var currentObjId = objectIndex++;
datastore.add(currentObjId);
datastore.setObjectData(currentObjId, state);
} else {
// create for the environment
if (datastore.exists(state["objid"])) {
// already create, then ignore this request
} else {
console.log("create object for environment", state["objid"]);
datastore.add(state["objid"]);
datastore.setObjectData(state["objid"], state);
}
}
// tell everyone else about this update, with entire list of objects
var dirtyObjects = {};
Object.keys(datastore.state["objects"]).forEach(function (key) {
// console.log(key, datastore.state["objects"][key]);
if (datastore.state["objects"][key]["dirty"]) {
datastore.state["objects"][key]["dirty"] = false;
}
dirtyObjects[key] = datastore.state["objects"][key];
});
const response = {
type: "object",
uid: key,
data: dirtyObjects,
success: true,
};
console.log(response);
send("*", -1, response);
break;
}
case "avatar": {
// console.log("receive avatar msg"); // console.log(json);
const userid = json["user"];
const state = json["state"];
avatars[userid] = {
user: userid,
state: state,
ts: Date.now(),
};
// console.log(avatars);
break;
}
case "webrtc": {
console.log("receive ws msg:", json["type"], json["ts"], Date.now());
// console.log(json["type"], avatars);
const key = json["uid"];
const state = json["state"];
// avatars[key].webrtc = state[uuid];
const response = {
type: "webrtc",
id: ws.index,
uid: key,
state: state,
ts: Date.now(),
success: true,
};
send("*", -1, response);
break;
}
case "test": {
console.log("receive test ", json["state"], "at", Date.now());
const response = {
type: "test",
id: ws.index,
ts: Date.now(),
state: state,
success: true,
};
send("*", -1, response);
break;
}
case "mute":
{
console.log(
"receive mute from ",
ws.index,
json["state"],
"at",
Date.now()
);
const response = {
type: "mute",
id: ws.index,
ts: Date.now(),
success: true,
state: json["state"],
};
send("*", ws.index, response);
}
break;
case "3d":
{
console.log("recv 3d data");
}
break;
default:
break;
}
});
ws.on("error", () => ws.terminate());
ws.on("close", () => {
console.log(".");
leave(ws.index);
});
});
wss.broadcast = function (data) {
this.clients.forEach(function (client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
console.log("Server running with port " + HTTPS_PORT);
// ----------------------------------------------------------------------------------------
// Separate server to redirect from http to https
// http.createServer(function (req, res) {
// console.log(req.headers['host'] + req.url);
// res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
// res.end();
// }).listen(HTTP_PORT);
timers["avatar"] = setInterval(() => {
if (Object.keys(avatars).length === 0) {
return;
}
// zhenyi
const response = {
type: "avatar",
data: avatars,
ts: Date.now(),
};
// console.log("timers[avatar] ", avatars);
send("*", -1, response);
}, AVATAR_RATE);
timers["object"] = setInterval(() => {
if (Object.keys(datastore.state["objects"]).length === 0) {
return;
}
// zhenyi
var dirtyObjects = {};
Object.keys(datastore.state["objects"]).forEach(function (key) {
// console.log(key, datastore.state["objects"][key]);
if (datastore.state["objects"][key]["dirty"]) {
datastore.state["objects"][key]["dirty"] = false;
dirtyObjects[key] = datastore.state["objects"][key];
}
});
const response = {
type: "object",
data: dirtyObjects,
ts: Date.now(),
};
// console.log("timers[avatar] ", avatars);
send("*", -1, response);
}, OBJECT_RATE);