client/scripts/test-network.js

75 lines
2.1 KiB
JavaScript

const WebSocket = require('ws');
const SERVER = process.argv[2] || 'wss://meet.srtk.in/ws';
const ROOM = 'diag-test';
const NAME = 'diag-bot-' + Math.floor(Math.random() * 1000);
console.log(`--- Network Diagnostic Tool ---`);
console.log(`Connecting to: ${SERVER}`);
console.log(`Room: ${ROOM}, Identity: ${NAME}`);
const url = `${SERVER}?room=${ROOM}&name=${NAME}`;
const ws = new WebSocket(url);
let startTime = Date.now();
let textPings = 0;
let binaryPings = 0;
let textAcks = 0;
let binaryAcks = 0;
ws.on('open', () => {
console.log('✅ WebSocket Connected');
// 1. Join
ws.send(JSON.stringify({
type: 'Join',
data: { room_code: ROOM, display_name: NAME }
}));
// 2. Start Test Cycle
setInterval(() => {
// Send Text Ping
ws.send(JSON.stringify({ type: 'Heartbeat' }));
textPings++;
// Send Binary Ping (1KB)
const dummy = Buffer.alloc(1024);
dummy[0] = 0xAA; // Diagnostic marker
ws.send(dummy);
binaryPings++;
console.log(`Stats: TX[Text:${textPings}, Bin:${binaryPings}] RX[Text:${textAcks}, Bin:${binaryAcks}]`);
}, 2000);
});
ws.on('message', (data, isBinary) => {
if (isBinary) {
binaryAcks++;
} else {
try {
const msg = JSON.parse(data.toString());
if (msg.type === 'Heartbeat') {
textAcks++;
} else if (msg.type === 'Joined') {
console.log('✅ Joined successfully! Self ID:', msg.data.self_id);
}
} catch (e) { }
}
});
ws.on('error', (err) => {
console.error('❌ WebSocket Error:', err.message);
});
ws.on('close', (code, reason) => {
console.log(`⚠️ Connection Closed (Code: ${code}, Reason: ${reason})`);
process.exit(0);
});
setTimeout(() => {
console.log('--- Test Finished ---');
console.log(`Summary:`);
console.log(`Text Pings: ${textPings}, Acks: ${textAcks} (${Math.round(textAcks / textPings * 100)}%)`);
console.log(`Binary Pings: ${binaryPings}, Acks: ${binaryAcks} (${Math.round(binaryAcks / binaryPings * 100)}%)`);
process.exit(0);
}, 20000);