Fix VideoTile bug and improve screen sharing

This commit is contained in:
srtk 2026-02-08 23:26:40 +05:30
parent d489873060
commit cade45d16d
7 changed files with 124 additions and 20 deletions

View file

@ -35,11 +35,14 @@ export class NetworkManager extends EventEmitter {
this.roomCode = roomCode; // Store for UDP handshake
return new Promise((resolve, reject) => {
// Determine Host and Protocol
let host = serverUrl.replace(/^wss?:\/\//, '').replace(/\/$/, '');
let host = serverUrl.trim().replace(/^wss?:\/\//, '').replace(/\/$/, '');
this.serverUdpHost = host.split(':')[0]; // Hostname only for UDP (strip port if present)
// Auto-detect protocol: localhost/IP uses ws://, domains use wss:// (HTTPS)
const isLocal = host.includes('localhost') || host.includes('127.0.0.1');
const isLocal = host.includes('localhost') ||
host.includes('127.0.0.1') ||
host.startsWith('192.168.') ||
host.startsWith('10.');
const protocol = isLocal ? 'ws' : 'wss';
const wsUrl = `${protocol}://${host}/ws`;
@ -141,16 +144,19 @@ export class NetworkManager extends EventEmitter {
this.ws.send(JSON.stringify(msg));
}
private heartbeatInterval: NodeJS.Timeout | null = null;
setupUdp() {
this.udp = dgram.createSocket('udp4');
this.udp.on('listening', () => {
const addr = this.udp?.address();
console.log(`UDP Listening on ${addr?.port}`);
this.sendHandshake();
this.startHeartbeat();
});
this.udp.on('message', (msg) => {
this.udp.on('message', (msg, rinfo) => {
console.log(`[UDP] Msg from ${rinfo.address}:${rinfo.port} - ${msg.length} bytes`);
this.handleUdpMessage(msg);
});
@ -286,9 +292,25 @@ export class NetworkManager extends EventEmitter {
}
}
startHeartbeat() {
if (this.heartbeatInterval) clearInterval(this.heartbeatInterval);
// Send immediately
this.sendHandshake();
// Send 3 bursts to ensure traversal
setTimeout(() => this.sendHandshake(), 500);
setTimeout(() => this.sendHandshake(), 1000);
// Keep-alive every 3 seconds
this.heartbeatInterval = setInterval(() => {
this.sendHandshake();
}, 3000);
}
sendHandshake() {
if (!this.udp || !this.userId || !this.roomCode) {
console.error('[UDP] Cannot send handshake: missing udp, userId, or roomCode');
// console.error('[UDP] Cannot send handshake: missing udp, userId, or roomCode');
return;
}
@ -311,13 +333,17 @@ export class NetworkManager extends EventEmitter {
const packet = Buffer.concat([header, payload]);
console.log(`[UDP] Sending Handshake: userId=${this.userId}, room=${this.roomCode}, ${packet.length} bytes`);
console.log(`[UDP] Sending Handshake: userId=${this.userId}, room=${this.roomCode}, ${packet.length} bytes to ${this.serverUdpHost}:${SERVER_UDP_PORT}`);
this.udp.send(packet, SERVER_UDP_PORT, this.serverUdpHost, (err) => {
if (err) console.error('UDP Handshake Send Error', err);
});
}
disconnect() {
if (this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
this.heartbeatInterval = null;
}
if (this.ws) this.ws.close();
if (this.udp) this.udp.close();
this.ws = null;