debug: Add detailed logging for SSE connection debugging
- Add timing logs for fetch requests to game server - Track SSE connection count and lifecycle - Add client-side logging for received messages - Log fetch response times and errors - Monitor connection establishment and closure
This commit is contained in:
@@ -12,6 +12,9 @@ const PORT = process.env.PORT || 3002;
|
|||||||
// Parse JSON bodies
|
// Parse JSON bodies
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
// Track SSE connections
|
||||||
|
let sseConnections = 0;
|
||||||
|
|
||||||
// Health check endpoint
|
// Health check endpoint
|
||||||
app.get('/health', (req, res) => {
|
app.get('/health', (req, res) => {
|
||||||
console.log('que pedos');
|
console.log('que pedos');
|
||||||
@@ -34,6 +37,10 @@ app.get('/api/config', (req, res) => {
|
|||||||
|
|
||||||
// SSE endpoint for real-time updates
|
// SSE endpoint for real-time updates
|
||||||
app.get('/api/sse', (req, res) => {
|
app.get('/api/sse', (req, res) => {
|
||||||
|
sseConnections++;
|
||||||
|
const connectionId = sseConnections;
|
||||||
|
console.log(`[SSE] New connection #${connectionId} established. Total: ${sseConnections}`);
|
||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'text/event-stream',
|
'Content-Type': 'text/event-stream',
|
||||||
'Cache-Control': 'no-cache',
|
'Cache-Control': 'no-cache',
|
||||||
@@ -47,38 +54,48 @@ app.get('/api/sse', (req, res) => {
|
|||||||
|
|
||||||
// Set up polling interval for game state updates
|
// Set up polling interval for game state updates
|
||||||
const pollInterval = setInterval(async () => {
|
const pollInterval = setInterval(async () => {
|
||||||
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
// Fetch game state from Colyseus server
|
// Fetch game state from Colyseus server
|
||||||
const gameServerUrl = process.env.SERVER_URL || 'http://localhost:2567';
|
const gameServerUrl = process.env.SERVER_URL || 'http://localhost:2567';
|
||||||
|
console.log(`[SSE] Fetching stats from: ${gameServerUrl}/api/admin/stats`);
|
||||||
|
|
||||||
const response = await fetch(`${gameServerUrl}/api/admin/stats`);
|
const response = await fetch(`${gameServerUrl}/api/admin/stats`);
|
||||||
|
|
||||||
|
const fetchTime = Date.now() - startTime;
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const gameStats = await response.json();
|
const gameStats = await response.json();
|
||||||
|
console.log(`[SSE] Stats fetched successfully in ${fetchTime}ms`);
|
||||||
res.write(`data: ${JSON.stringify({
|
res.write(`data: ${JSON.stringify({
|
||||||
type: 'gameStats',
|
type: 'gameStats',
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
data: gameStats
|
data: gameStats
|
||||||
})}\n\n`);
|
})}\n\n`);
|
||||||
} else {
|
} else {
|
||||||
|
console.log(`[SSE] Stats fetch failed with status ${response.status} in ${fetchTime}ms`);
|
||||||
// Send error status if server is not reachable
|
// Send error status if server is not reachable
|
||||||
res.write(`data: ${JSON.stringify({
|
res.write(`data: ${JSON.stringify({
|
||||||
type: 'error',
|
type: 'error',
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
message: 'Cannot connect to game server'
|
message: `Cannot connect to game server (${response.status})`
|
||||||
})}\n\n`);
|
})}\n\n`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching game stats:', error);
|
const fetchTime = Date.now() - startTime;
|
||||||
|
console.error(`[SSE] Error fetching game stats in ${fetchTime}ms:`, error.message);
|
||||||
res.write(`data: ${JSON.stringify({
|
res.write(`data: ${JSON.stringify({
|
||||||
type: 'error',
|
type: 'error',
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
message: 'Error fetching game stats'
|
message: `Error fetching game stats: ${error.message}`
|
||||||
})}\n\n`);
|
})}\n\n`);
|
||||||
}
|
}
|
||||||
}, 500); // Poll every 500ms
|
}, 500); // Poll every 500ms
|
||||||
|
|
||||||
// Clean up on client disconnect
|
// Clean up on client disconnect
|
||||||
req.on('close', () => {
|
req.on('close', () => {
|
||||||
|
sseConnections--;
|
||||||
|
console.log(`[SSE] Connection #${connectionId} closed. Total: ${sseConnections}`);
|
||||||
clearInterval(pollInterval);
|
clearInterval(pollInterval);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ class AdminService {
|
|||||||
this.eventSource = new EventSource('/api/sse')
|
this.eventSource = new EventSource('/api/sse')
|
||||||
|
|
||||||
this.eventSource.onopen = () => {
|
this.eventSource.onopen = () => {
|
||||||
|
console.log('[AdminService] SSE connection opened')
|
||||||
this.isConnected = true
|
this.isConnected = true
|
||||||
this.connectionCallback?.(true)
|
this.connectionCallback?.(true)
|
||||||
}
|
}
|
||||||
@@ -57,14 +58,15 @@ class AdminService {
|
|||||||
this.eventSource.onmessage = (event) => {
|
this.eventSource.onmessage = (event) => {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
|
console.log('[AdminService] SSE message received:', data.type, data.timestamp)
|
||||||
this.callback?.(data)
|
this.callback?.(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error parsing SSE message:', error)
|
console.error('[AdminService] Error parsing SSE message:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.eventSource.onerror = (error) => {
|
this.eventSource.onerror = (error) => {
|
||||||
console.error('SSE connection error:', error)
|
console.error('[AdminService] SSE connection error:', error)
|
||||||
this.isConnected = false
|
this.isConnected = false
|
||||||
this.connectionCallback?.(false)
|
this.connectionCallback?.(false)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user