-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-heartbeat.js
More file actions
67 lines (58 loc) · 2.09 KB
/
Copy pathtest-heartbeat.js
File metadata and controls
67 lines (58 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Simple test script to verify heartbeat mechanism
* Run this script to test the connection health monitoring
*/
import { getConnectionHealthStats } from './socket/socketController.js';
import express from 'express';
import { createServer } from 'http';
import { setupWebSocket } from './socket/socketSetup.js';
// Create minimal Express app for testing
const app = express();
const server = createServer(app);
// Setup WebSocket
setupWebSocket(server);
// Health check endpoint
app.get('/health/connections', (req, res) => {
const stats = getConnectionHealthStats();
res.json({
timestamp: new Date().toISOString(),
...stats
});
});
// Start server
const PORT = process.env.PORT || 5001;
server.listen(PORT, () => {
console.log(`🔧 Heartbeat test server running on port ${PORT}`);
console.log(`📊 Connection health stats available at: http://localhost:${PORT}/health/connections`);
// Log connection stats every 30 seconds
setInterval(() => {
const stats = getConnectionHealthStats();
console.log('📊 Connection Health Stats:', {
totalSockets: stats.totalTrackedSockets,
totalUsers: stats.totalConnectedUsers,
timestamp: new Date().toISOString()
});
if (Object.keys(stats.healthStatus).length > 0) {
console.log('💓 Health Details:');
Object.entries(stats.healthStatus).forEach(([socketId, health]) => {
const status = health.isHealthy ? '✅' : '⚠️';
console.log(` ${status} ${socketId}: ${health.lastPingAgo}ms ago (missed: ${health.missedPings})`);
});
}
}, 30000);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('🛑 Shutting down heartbeat test server...');
server.close(() => {
console.log('✅ Server closed gracefully');
process.exit(0);
});
});
process.on('SIGINT', () => {
console.log('🛑 Shutting down heartbeat test server...');
server.close(() => {
console.log('✅ Server closed gracefully');
process.exit(0);
});
});