Skip to content

Commit

Permalink
edit server code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonghakseo committed Aug 20, 2022
1 parent 0d6e295 commit 2b0e63d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/reload/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ import { WebSocketServer, WebSocket } from "ws";
import chokidar from "chokidar";
import { clearTimeout } from "timers";

const port = 8081;
const wss = new WebSocketServer({ port });
const PORT = 8081;
const wss = new WebSocketServer({ port: PORT });

const wsSet: Set<WebSocket> = new Set();

console.log("ws server:" + port);
console.log("ws server:" + PORT);

wss.on("connection", (ws) => {
ws.addEventListener("message", (event) => {
if (event.data === "add") {
wsSet.add(ws);
}
if (event.data === "remove") {
wsSet.delete(ws);
switch (event.data) {
case "add":
return wsSet.add(ws);
case "remove":
return wsSet.delete(ws);
}
});
});

let timer;
let timer: NodeJS.Timeout;
const DELAY = 200;
chokidar.watch("dist").on("all", () => {
clearTimeout(timer);
// debounce
timer = setTimeout(() => {
wsSet.forEach((ws) => ws.send("update"));
}, 200);
}, DELAY);
});

0 comments on commit 2b0e63d

Please sign in to comment.