fix(web): close a socket whose send throws
Review of PR #183: the socket was dropped from the client set but left open, outside the heartbeat, so its page was never notified again. terminate() closes it and the page reconnects through onclose. Refs #177 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b03f070a51
commit
cc591ba85e
2 changed files with 6 additions and 1 deletions
|
|
@ -12,6 +12,7 @@ type FakeSocket = EventEmitter & {
|
|||
userId: string;
|
||||
readyState: number;
|
||||
send: ReturnType<typeof vi.fn>;
|
||||
terminate: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
const OPEN = 1; // WebSocket.OPEN
|
||||
|
|
@ -22,7 +23,7 @@ const g = globalThis as unknown as {
|
|||
};
|
||||
|
||||
function fakeSocket(userId: string): FakeSocket {
|
||||
return Object.assign(new EventEmitter(), { userId, readyState: OPEN, send: vi.fn() });
|
||||
return Object.assign(new EventEmitter(), { userId, readyState: OPEN, send: vi.fn(), terminate: vi.fn() });
|
||||
}
|
||||
|
||||
async function freshModule() {
|
||||
|
|
@ -106,6 +107,7 @@ describe('notifyUserChanged', () => {
|
|||
ws.notifyUserChanged('user-a');
|
||||
expect(() => vi.advanceTimersByTime(ws.NOTIFY_DEBOUNCE_MS)).not.toThrow();
|
||||
expect(ws.getActiveConnections()).toBe(0);
|
||||
expect(a.terminate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -171,7 +171,10 @@ export function broadcastToUser(userId: string, message: WsMessage) {
|
|||
try {
|
||||
ws.send(payload);
|
||||
} catch {
|
||||
// Close it too: left open, it would escape the heartbeat and the page
|
||||
// would never be notified again (its onclose reconnects it).
|
||||
clients.delete(ws);
|
||||
ws.terminate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue