Skip to content

Commit

Permalink
feat: add logging for WebSocket connection events and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinkrustev committed Feb 26, 2025
1 parent 7a7b9e1 commit d1e10be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mono-log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ const init = async () => {
const wss = new WebSocketServer({ server: server.listener });

function heartbeat() {
console.log(`WebSocket heartbeat: ${this?._socket?.remoteAddress}:${this?._socket?.remotePort}`);
this.isAlive = true;
}

wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);
ws.on('close', function wsClose(code, reason) {
console.log(`WebSocket ${ws?._socket?.remoteAddress}:${ws?._socket?.remotePort} closed`, code, reason);
});
});

const interval = setInterval(function ping() {
Expand Down
6 changes: 6 additions & 0 deletions mono-log/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const App = () => {
}
setLogMessages((prevMessages) => [logMessage, ...prevMessages]);
};
ws.current.onclose = event => {
console.log('WebSocket closed', event);
};
ws.current.onerror = event => {
console.error('WebSocket error', event);
};

return () => {
ws.current.close();
Expand Down

0 comments on commit d1e10be

Please sign in to comment.