-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
62 lines (51 loc) · 1.8 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
const { StreamingMediaServer } = require("./StreamingMediaServer");
const RoomCodeAuth = require("./RoomCodeAuth");
const ControlServer = require("./ControlServer");
const express = require("express");
const http = require("http");
const baseDir = require("path").dirname(require.resolve("./package.json"));
var cors = require("cors");
const frontendPort = process.env.PORT || 3001,
app = express(),
frontend = http.createServer(app),
auth = new RoomCodeAuth(),
streamingMediaServer = new StreamingMediaServer({
streamingPath: "/stream",
mediaSinkPath: `${baseDir}/build/server/share-space-media-sink.sock`,
infoSinkPath: `${baseDir}/build/server/share-space-info-sink.sock`
}),
controlServer = new ControlServer("/control");
//// HTTP
// Order of middleware matters.
app.use(auth.middleware());
app.use(express.json());
// No-auth middleware
app.use("/assets", express.static("assets"));
// Auth-required middleware
app.use("/", auth.requireAuth());
app.use("/", (req, res) => res.sendStatus(404));
//// WebSocket
frontend.on("upgrade", (request, socket, head) => {
auth.checkAuth(request, (err, user) => {
if (err) {
console.log("error authenticating", err);
socket.destroy();
return;
}
console.log("upgrade authenticated", user);
if (streamingMediaServer.shouldHandle(request)) {
streamingMediaServer.handleUpgrade(request, socket, head);
} else if (controlServer.shouldHandle(request)) {
controlServer.handleUpgrade(request, socket, head);
} else {
console.log("Rejected upgrade request");
socket.destroy();
}
});
});
//// Listen
streamingMediaServer.start();
controlServer.start();
frontend.listen(frontendPort, () => {
console.log(`Find the server at: http://localhost:${frontendPort}/`); // eslint-disable-line no-console
});