|
@@ -1,6 +1,11 @@
|
1
|
1
|
import express from "express";
|
2
|
2
|
import http, { ServerResponse } from "http";
|
3
|
3
|
import socketIO from "socket.io";
|
|
4
|
+import debug from "debug";
|
|
5
|
+
|
|
6
|
+const serverDebug = debug("server");
|
|
7
|
+const ioDebug = debug("io");
|
|
8
|
+const socketDebug = debug("socket");
|
4
|
9
|
|
5
|
10
|
const app = express();
|
6
|
11
|
const port = process.env.PORT || 80; // default port to listen
|
|
@@ -8,7 +13,7 @@ const port = process.env.PORT || 80; // default port to listen
|
8
|
13
|
const server = http.createServer(app);
|
9
|
14
|
|
10
|
15
|
server.listen(port, () => {
|
11
|
|
- console.log(`listening on port: ${port}`);
|
|
16
|
+ serverDebug(`listening on port: ${port}`);
|
12
|
17
|
});
|
13
|
18
|
|
14
|
19
|
const io = socketIO(server, {
|
|
@@ -24,10 +29,10 @@ const io = socketIO(server, {
|
24
|
29
|
});
|
25
|
30
|
|
26
|
31
|
io.on("connection", socket => {
|
27
|
|
- console.log("connection established!");
|
|
32
|
+ ioDebug("connection established!");
|
28
|
33
|
io.to(`${socket.id}`).emit("init-room");
|
29
|
34
|
socket.on("join-room", roomID => {
|
30
|
|
- console.log(`${socket.id} has joined ${roomID}`);
|
|
35
|
+ socketDebug(`${socket.id} has joined ${roomID}`);
|
31
|
36
|
socket.join(roomID);
|
32
|
37
|
if (io.sockets.adapter.rooms[roomID].length <= 1) {
|
33
|
38
|
io.to(`${socket.id}`).emit("first-in-room");
|
|
@@ -43,7 +48,7 @@ io.on("connection", socket => {
|
43
|
48
|
socket.on(
|
44
|
49
|
"server-broadcast",
|
45
|
50
|
(roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => {
|
46
|
|
- console.log(`${socket.id} sends update to ${roomID}`);
|
|
51
|
+ socketDebug(`${socket.id} sends update to ${roomID}`);
|
47
|
52
|
socket.broadcast.to(roomID).emit("client-broadcast", encryptedData, iv);
|
48
|
53
|
}
|
49
|
54
|
);
|
|
@@ -51,7 +56,7 @@ io.on("connection", socket => {
|
51
|
56
|
socket.on(
|
52
|
57
|
"server-volatile-broadcast",
|
53
|
58
|
(roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => {
|
54
|
|
- console.log(`${socket.id} sends volatile update to ${roomID}`);
|
|
59
|
+ socketDebug(`${socket.id} sends volatile update to ${roomID}`);
|
55
|
60
|
socket.volatile.broadcast
|
56
|
61
|
.to(roomID)
|
57
|
62
|
.emit("client-broadcast", encryptedData, iv);
|