-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
98 lines (83 loc) · 3.45 KB
/
index.ts
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
import fs from "fs/promises"
import http from "http"
import { WebSocket, WebSocketServer } from "ws"
import { v4 as uuid } from "uuid"
import User from "./User.js"
import { getContentType } from "./utils.js"
import { WSMessage } from "./types.js"
const port = process.env.port || 80;
const WaitingRoom: User[] = []
// Server 💻
const server = http.createServer((req, res) => {
if (req.headers["fly-forwarded-proto"] == "http") {
res.writeHead(302, { Location: `https://${req.headers.host}${req.url}` });
res.end();
return false;
}
const requestURL = new URL(req.url!, `https://${req.headers.host}`);
/**
* Sends a file as response
* @param {import("fs").PathLike} path Path to the file
* @param {object} [options] Additional options
* @param {number} [options.statusCode]
*/
const sendFile = async (path: import("fs").PathLike, options?: { statusCode?: number; }) => {
res.statusCode = options?.statusCode || 200;
res.setHeader("Content-Type", getContentType(path)!);
try {
res.end(await fs.readFile(path));
} catch (e) {
res.statusCode = 404;
res.end();
}
}
// CORS 🔒
res.setHeader("Access-Control-Allow-Origin", "https://lovers-canvas.glitch.me");
res.setHeader("Access-Control-Allow-Methods", "GET, POST");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Headers", "*");
if (req.method == "GET") return sendFile(requestURL.pathname == "/" ? "./app/index.html" : (("./app" + req.url)));
});
// @ts-ignore
server.listen(port, "0.0.0.0", () => {
console.log("👍");
});
// WebSocket 🔌
const wss = new WebSocketServer({ server });
wss.on("connection", (socket) => {
console.log("🤗");
let user: User
socket.on("message", d => {
let data: WSMessage = JSON.parse(d.toString());
if (data.type == "system" && data.content == "register") {
user = new User(uuid(), data.value, socket)
WaitingRoom.push(user)
user.send("system", "id", user.id)
console.log(`${user.id} joined waiting room`);
let partner = WaitingRoom.find(u => u.id != user.id && u.gender == user.interestedIn && u.interestedIn == user.gender)
if (!partner) return
let optimalScreenSize = Math.sign((user.screenSize.width + user.screenSize.height) - (partner.screenSize.width + partner.screenSize.height)) == 1 ? partner.screenSize : user.screenSize
partner.send("system", "partner", { id: user.id, screenSize: optimalScreenSize, sendOffer: true })
user.send("system", "partner", { id: partner?.id, screenSize: optimalScreenSize, sendOffer: false })
} else if (user) {
data.content != "ping" && console.log(data);
switch (data.type) {
case "system":
switch (data.content) {
case "ping": user.send("system", "pong", "pong");break;
}
break;
case "signaling":
// @ts-ignore
WaitingRoom.find(u => u.id == data.to)?.send("signaling", data.content, data.value, user.id);
break;
}
}
})
socket.on("close", () => {
if (user) {
WaitingRoom.splice(WaitingRoom.indexOf(user), 1);
console.log("🙋♂️");
}
});
});