Skip to content

Commit 1301831

Browse files
committed
add debug cause im confused
1 parent b6dae54 commit 1301831

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

server/websockets/flightsWebsocket.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export function setupFlightsWebsocket(httpServer) {
2222
const sessionId = socket.handshake.query.sessionId;
2323
const accessId = socket.handshake.query.accessId;
2424

25+
console.log(`[Flights Socket] Connection attempt - SessionID: ${sessionId || 'missing'}, AccessID: ${accessId ? 'provided' : 'missing'}, Socket ID: ${socket.id}`);
26+
2527
if (!sessionId) {
28+
console.warn(`[Flights Socket] Rejected connection ${socket.id} - Missing sessionId`);
2629
socket.disconnect(true);
2730
return;
2831
}
@@ -31,13 +34,15 @@ export function setupFlightsWebsocket(httpServer) {
3134
if (accessId) {
3235
const valid = await validateSessionAccess(sessionId, accessId);
3336
if (!valid) {
37+
console.warn(`[Flights Socket] Rejected connection ${socket.id} - Invalid session access for sessionId: ${sessionId}`);
3438
socket.disconnect(true);
3539
return;
3640
}
3741
role = 'controller';
3842
}
3943
socket.data.role = role;
4044

45+
console.log(`[Flights Socket] Connection accepted - SessionID: ${sessionId}, Role: ${role}, Socket ID: ${socket.id}`);
4146
socket.join(sessionId);
4247

4348
socket.on('updateFlight', async ({ flightId, updates }) => {
@@ -227,6 +232,10 @@ export function setupFlightsWebsocket(httpServer) {
227232
socket.emit('flightError', { action: 'contactMe', flightId, error: 'Failed to send contact message' });
228233
}
229234
});
235+
236+
socket.on('disconnect', (reason) => {
237+
console.log(`[Flights Socket] Disconnected - SessionID: ${sessionId}, Socket ID: ${socket.id}, Reason: ${reason}`);
238+
});
230239
});
231240

232241
return io;

src/pages/Flights.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export default function Flights() {
271271
return () => {
272272
socket.socket.disconnect();
273273
};
274-
}, [sessionId, accessId, initialLoadComplete, user, settings, accessError]);
274+
}, [sessionId, accessId, initialLoadComplete, user, settings]);
275275
const handleIssuePDC = async (
276276
flightId: string | number,
277277
pdcText: string
@@ -474,11 +474,12 @@ export default function Flights() {
474474
(f) => f.id === flightId
475475
);
476476

477-
if (isExternalArrival && arrivalsSocket) {
477+
if (isExternalArrival && arrivalsSocket?.socket?.connected) {
478478
arrivalsSocket.updateArrival(flightId, updates);
479-
} else if (flightsSocket) {
479+
} else if (flightsSocket?.socket?.connected) {
480480
flightsSocket.updateFlight(flightId, updates);
481481
} else {
482+
console.warn('Socket not connected, updating local state only');
482483
setFlights((prev) =>
483484
prev.map((flight) =>
484485
flight.id === flightId ? { ...flight, ...updates } : flight
@@ -511,9 +512,10 @@ export default function Flights() {
511512
}
512513

513514
// Regular flight deletion via WebSocket
514-
if (flightsSocket) {
515+
if (flightsSocket?.socket?.connected) {
515516
flightsSocket.deleteFlight(flightId);
516517
} else {
518+
console.warn('Socket not connected, updating local state only');
517519
setFlights((prev) =>
518520
prev.filter((flight) => flight.id !== flightId)
519521
);
@@ -573,8 +575,10 @@ export default function Flights() {
573575
setSession((prev) =>
574576
prev ? { ...prev, activeRunway: selectedRunway } : null
575577
);
576-
if (flightsSocket) {
578+
if (flightsSocket?.socket?.connected) {
577579
flightsSocket.updateSession({ activeRunway: selectedRunway });
580+
} else {
581+
console.warn('Socket not connected, runway updated via API only');
578582
}
579583
} catch (error) {
580584
console.error('Failed to update runway:', error);

src/sockets/flightsSocket.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,34 @@ export function createFlightsSocket(
1414
const socket = io(SOCKET_URL, {
1515
withCredentials: true,
1616
path: '/sockets/flights',
17-
query: { sessionId, accessId }
17+
query: { sessionId, accessId },
18+
reconnection: true,
19+
reconnectionDelay: 1000,
20+
reconnectionAttempts: 5,
21+
timeout: 10000
22+
});
23+
24+
// Add error handlers for better debugging
25+
socket.on('connect_error', (error) => {
26+
console.error('Socket connection error:', error.message);
27+
});
28+
29+
socket.on('error', (error) => {
30+
console.error('Socket error:', error);
31+
});
32+
33+
socket.on('disconnect', (reason) => {
34+
console.log('Socket disconnected:', reason);
35+
if (reason === 'io server disconnect') {
36+
// Server disconnected the socket, try to reconnect manually
37+
socket.connect();
38+
}
1839
});
1940

2041
socket.on('flightUpdated', onFlightUpdated);
2142
socket.on('flightAdded', onFlightAdded);
2243
socket.on('flightDeleted', onFlightDeleted);
23-
44+
2445
if (onFlightError) {
2546
socket.on('flightError', onFlightError);
2647
}

0 commit comments

Comments
 (0)