mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-20 15:06:15 -04:00
fix keepAliveTimers not correctly cleared on reconnect
This commit is contained in:
parent
02911804cb
commit
6679ef7529
1 changed files with 27 additions and 16 deletions
43
index.js
43
index.js
|
@ -130,8 +130,10 @@ class PairDropServer {
|
||||||
this._wss = new WebSocket.Server({ server });
|
this._wss = new WebSocket.Server({ server });
|
||||||
this._wss.on('connection', (socket, request) => this._onConnection(new Peer(socket, request)));
|
this._wss.on('connection', (socket, request) => this._onConnection(new Peer(socket, request)));
|
||||||
|
|
||||||
this._rooms = {};
|
this._rooms = {}; // { roomId: peers[] }
|
||||||
this._roomSecrets = {};
|
this._roomSecrets = {}; // { pairKey: roomSecret }
|
||||||
|
|
||||||
|
this._keepAliveTimers = {};
|
||||||
|
|
||||||
console.log('PairDrop is running on port', port);
|
console.log('PairDrop is running on port', port);
|
||||||
}
|
}
|
||||||
|
@ -139,7 +141,9 @@ class PairDropServer {
|
||||||
_onConnection(peer) {
|
_onConnection(peer) {
|
||||||
peer.socket.on('message', message => this._onMessage(peer, message));
|
peer.socket.on('message', message => this._onMessage(peer, message));
|
||||||
peer.socket.onerror = e => console.error(e);
|
peer.socket.onerror = e => console.error(e);
|
||||||
|
|
||||||
this._keepAlive(peer);
|
this._keepAlive(peer);
|
||||||
|
|
||||||
this._send(peer, {
|
this._send(peer, {
|
||||||
type: 'rtc-config',
|
type: 'rtc-config',
|
||||||
config: rtcConfig
|
config: rtcConfig
|
||||||
|
@ -170,7 +174,7 @@ class PairDropServer {
|
||||||
this._onDisconnect(sender);
|
this._onDisconnect(sender);
|
||||||
break;
|
break;
|
||||||
case 'pong':
|
case 'pong':
|
||||||
sender.lastBeat = Date.now();
|
this._keepAliveTimers[sender.id].lastBeat = Date.now();
|
||||||
break;
|
break;
|
||||||
case 'join-ip-room':
|
case 'join-ip-room':
|
||||||
this._joinRoom(sender);
|
this._joinRoom(sender);
|
||||||
|
@ -223,10 +227,15 @@ class PairDropServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_disconnect(sender) {
|
_disconnect(sender) {
|
||||||
this._leaveRoom(sender, 'ip', '', true);
|
|
||||||
this._leaveAllSecretRooms(sender, true);
|
|
||||||
this._removeRoomKey(sender.roomKey);
|
this._removeRoomKey(sender.roomKey);
|
||||||
sender.roomKey = null;
|
sender.roomKey = null;
|
||||||
|
|
||||||
|
this._cancelKeepAlive(sender);
|
||||||
|
delete this._keepAliveTimers[sender.id];
|
||||||
|
|
||||||
|
this._leaveRoom(sender, 'ip', '', true);
|
||||||
|
this._leaveAllSecretRooms(sender, true);
|
||||||
|
|
||||||
sender.socket.terminate();
|
sender.socket.terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -465,23 +474,29 @@ class PairDropServer {
|
||||||
|
|
||||||
_keepAlive(peer) {
|
_keepAlive(peer) {
|
||||||
this._cancelKeepAlive(peer);
|
this._cancelKeepAlive(peer);
|
||||||
let timeout = 500;
|
let timeout = 1000;
|
||||||
if (!peer.lastBeat) {
|
|
||||||
peer.lastBeat = Date.now();
|
if (!this._keepAliveTimers[peer.id]) {
|
||||||
|
this._keepAliveTimers[peer.id] = {
|
||||||
|
timer: 0,
|
||||||
|
lastBeat: Date.now()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if (Date.now() - peer.lastBeat > 2 * timeout) {
|
|
||||||
|
if (Date.now() - this._keepAliveTimers[peer.id].lastBeat > 2 * timeout) {
|
||||||
|
// Disconnect peer if unresponsive for 10s
|
||||||
this._disconnect(peer);
|
this._disconnect(peer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._send(peer, { type: 'ping' });
|
this._send(peer, { type: 'ping' });
|
||||||
|
|
||||||
peer.timerId = setTimeout(() => this._keepAlive(peer), timeout);
|
this._keepAliveTimers[peer.id].timer = setTimeout(() => this._keepAlive(peer), timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
_cancelKeepAlive(peer) {
|
_cancelKeepAlive(peer) {
|
||||||
if (peer && peer.timerId) {
|
if (this._keepAliveTimers[peer.id]?.timer) {
|
||||||
clearTimeout(peer.timerId);
|
clearTimeout(this._keepAliveTimers[peer.id].timer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -506,10 +521,6 @@ class Peer {
|
||||||
// set name
|
// set name
|
||||||
this._setName(request);
|
this._setName(request);
|
||||||
|
|
||||||
// for keepalive
|
|
||||||
this.timerId = 0;
|
|
||||||
this.lastBeat = Date.now();
|
|
||||||
|
|
||||||
this.roomSecrets = [];
|
this.roomSecrets = [];
|
||||||
this.roomKey = null;
|
this.roomKey = null;
|
||||||
this.roomKeyRate = 0;
|
this.roomKeyRate = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue