1. Server: WebSocket ping/pong heartbeat (index.ts) Added a 30-second ping interval that pings all connected clients Connections without a pong response for 45 seconds are terminated and cleaned up Extracted removeDeadConnection() to deduplicate the cleanup logic between close events and dead connection reaping 2. Server: Fixed sendServerUsers filter bug (handler.ts:13) Removed && cu.displayName from the filter — users who joined a server before their identify message was processed were silently invisible to everyone. This was the direct cause of "can't see each other" in session 2. 3. Client: Typing message now includes serverId Added serverId: this.webrtc.currentServerId to the typing payload Added a currentServerId getter on WebRTCService
97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
import {
|
|
IncomingMessage,
|
|
Server,
|
|
ServerResponse
|
|
} from 'http';
|
|
import { WebSocketServer, WebSocket } from 'ws';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { connectedUsers } from './state';
|
|
import { broadcastToServer } from './broadcast';
|
|
import { handleWebSocketMessage } from './handler';
|
|
|
|
/** How often to ping all connected clients (ms). */
|
|
const PING_INTERVAL_MS = 30_000;
|
|
/** Maximum time a client can go without a pong before we consider it dead (ms). */
|
|
const PONG_TIMEOUT_MS = 45_000;
|
|
|
|
function removeDeadConnection(connectionId: string): void {
|
|
const user = connectedUsers.get(connectionId);
|
|
|
|
if (user) {
|
|
console.log(`Removing dead connection: ${user.displayName ?? 'Unknown'} (${user.oderId})`);
|
|
|
|
user.serverIds.forEach((sid) => {
|
|
broadcastToServer(sid, {
|
|
type: 'user_left',
|
|
oderId: user.oderId,
|
|
displayName: user.displayName,
|
|
serverId: sid
|
|
}, user.oderId);
|
|
});
|
|
|
|
try {
|
|
user.ws.terminate();
|
|
} catch {
|
|
console.warn(`Failed to terminate WebSocket for ${user.displayName ?? 'Unknown'} (${user.oderId})`);
|
|
}
|
|
}
|
|
|
|
connectedUsers.delete(connectionId);
|
|
}
|
|
|
|
export function setupWebSocket(server: Server<typeof IncomingMessage, typeof ServerResponse>): void {
|
|
const wss = new WebSocketServer({ server });
|
|
// Periodically ping all clients and reap dead connections
|
|
const pingInterval = setInterval(() => {
|
|
const now = Date.now();
|
|
|
|
connectedUsers.forEach((user, connectionId) => {
|
|
if (now - user.lastPong > PONG_TIMEOUT_MS) {
|
|
removeDeadConnection(connectionId);
|
|
return;
|
|
}
|
|
|
|
if (user.ws.readyState === WebSocket.OPEN) {
|
|
try {
|
|
user.ws.ping();
|
|
} catch {
|
|
console.warn(`Failed to ping client ${user.displayName ?? 'Unknown'} (${user.oderId})`);
|
|
}
|
|
}
|
|
});
|
|
}, PING_INTERVAL_MS);
|
|
|
|
wss.on('close', () => clearInterval(pingInterval));
|
|
|
|
wss.on('connection', (ws: WebSocket) => {
|
|
const connectionId = uuidv4();
|
|
const now = Date.now();
|
|
|
|
connectedUsers.set(connectionId, { oderId: connectionId, ws, serverIds: new Set(), lastPong: now });
|
|
|
|
ws.on('pong', () => {
|
|
const user = connectedUsers.get(connectionId);
|
|
|
|
if (user) {
|
|
user.lastPong = Date.now();
|
|
}
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
try {
|
|
const message = JSON.parse(data.toString());
|
|
|
|
handleWebSocketMessage(connectionId, message);
|
|
} catch (err) {
|
|
console.error('Invalid WebSocket message:', err);
|
|
}
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
removeDeadConnection(connectionId);
|
|
});
|
|
|
|
ws.send(JSON.stringify({ type: 'connected', connectionId, serverTime: Date.now() }));
|
|
});
|
|
}
|