Skip to content

Commit

Permalink
Remove the server side check of whether all WS clients are connected
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 29, 2025
1 parent 359103f commit 7c4bf47
Showing 1 changed file with 1 addition and 20 deletions.
21 changes: 1 addition & 20 deletions code/core/src/core-server/utils/get-server-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ export class ServerChannelTransport {

private handler?: ChannelHandler;

isAlive = false;

private heartbeat() {
this.isAlive = true;
}

constructor(server: Server) {
this.heartbeat = this.heartbeat.bind(this);

this.socket = new WebSocketServer({ noServer: true });

server.on('upgrade', (request, socket, head) => {
Expand All @@ -36,29 +28,18 @@ export class ServerChannelTransport {
}
});
this.socket.on('connection', (wss) => {
this.isAlive = true;
wss.on('message', (raw) => {
const data = raw.toString();
const event =
typeof data === 'string' && isJSON(data)
? parse(data, { allowFunction: false, allowClass: false })
: data;
this.handler?.(event);
if (event.type === 'pong') {
this.heartbeat();
}
});
});

const interval = setInterval(() => {
this.socket.clients.forEach((ws) => {
if (this.isAlive === false) {
return ws.terminate();
}

this.isAlive = false;
this.send({ type: 'ping' });
});
this.send({ type: 'ping' });
}, HEARTBEAT_INTERVAL);

this.socket.on('close', function close() {
Expand Down

0 comments on commit 7c4bf47

Please sign in to comment.