Просмотр исходного кода

add debug library and replace console.logs

master
idlewinn 5 лет назад
Родитель
Сommit
230ccd1baf
3 измененных файлов: 19 добавлений и 7 удалений
  1. 3
    1
      package.json
  2. 10
    5
      src/index.ts
  3. 6
    1
      yarn.lock

+ 3
- 1
package.json Просмотреть файл

@@ -24,7 +24,7 @@
24 24
   },
25 25
   "homepage": "https://github.com/excalidraw/excalidraw-room#readme",
26 26
   "dependencies": {
27
-    "@types/socket.io": "^2.1.4",
27
+    "debug": "^4.1.1",
28 28
     "eslint": "6.8.0",
29 29
     "eslint-config-prettier": "6.10.0",
30 30
     "eslint-plugin-prettier": "3.1.2",
@@ -33,6 +33,8 @@
33 33
     "socket.io": "^2.3.0"
34 34
   },
35 35
   "devDependencies": {
36
+    "@types/socket.io": "^2.1.4",
37
+    "@types/debug": "^4.1.5",
36 38
     "@types/express": "^4.17.3",
37 39
     "@types/node": "^13.9.0",
38 40
     "typescript": "^3.8.3"

+ 10
- 5
src/index.ts Просмотреть файл

@@ -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);

+ 6
- 1
yarn.lock Просмотреть файл

@@ -38,6 +38,11 @@
38 38
   dependencies:
39 39
     "@types/node" "*"
40 40
 
41
+"@types/debug@^4.1.5":
42
+  version "4.1.5"
43
+  resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
44
+  integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==
45
+
41 46
 "@types/express-serve-static-core@*":
42 47
   version "4.17.2"
43 48
   resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.2.tgz#f6f41fa35d42e79dbf6610eccbb2637e6008a0cf"
@@ -371,7 +376,7 @@ debug@2.6.9:
371 376
   dependencies:
372 377
     ms "2.0.0"
373 378
 
374
-debug@^4.0.1, debug@~4.1.0:
379
+debug@^4.0.1, debug@^4.1.1, debug@~4.1.0:
375 380
   version "4.1.1"
376 381
   resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
377 382
   integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==

Загрузка…
Отмена
Сохранить