Skip to content

Commit

Permalink
Backend: respond to close events in the client.
Browse files Browse the repository at this point in the history
  • Loading branch information
paveloom committed Sep 20, 2024
1 parent 10b6dde commit 1565c06
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions backend/client/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function cleanup() {
process.exit();
}

function respond(value) {
replServer.setPrompt("< ");
replServer.displayPrompt();
console.log(value);
replServer.setPrompt("> ");
replServer.displayPrompt();
}

const replServer = repl.start();
replServer.context.ws = ws;
replServer.ignoreUndefined = true;
Expand All @@ -28,11 +36,14 @@ replServer.on("SIGINT", () => {
});

ws.onmessage = (event) => {
replServer.setPrompt("< ");
replServer.displayPrompt();
console.log(event.data);
replServer.setPrompt("> ");
replServer.displayPrompt();
respond(event.data);
};

ws.onclose = (event) => {
let code = event.code;
let reason = event.reason;
let clean = event.wasClean ? "clean" : "not clean";
respond(`Connection closed (${code}, "${reason}", ${clean})`);
};

ws.onerror = (event) => {
Expand Down

0 comments on commit 1565c06

Please sign in to comment.