stability on reconnect: prevent "peer-left" signal after "peer-joined" by leaving rooms first before reentering them, clear _keepAlive timeout before joining ip room and not manually terminating sockets
This commit is contained in:
parent
bbb8c1b10f
commit
cf715b2872
1 changed files with 6 additions and 6 deletions
12
index.js
12
index.js
|
@ -1,6 +1,7 @@
|
||||||
const process = require('process')
|
const process = require('process')
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
const {spawn} = require('child_process')
|
const {spawn} = require('child_process')
|
||||||
|
const WebSocket = require('ws');
|
||||||
|
|
||||||
// Handle SIGINT
|
// Handle SIGINT
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
|
@ -99,7 +100,6 @@ const { uniqueNamesGenerator, animals, colors } = require('unique-names-generato
|
||||||
class PairDropServer {
|
class PairDropServer {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const WebSocket = require('ws');
|
|
||||||
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)));
|
||||||
|
|
||||||
|
@ -110,10 +110,10 @@ class PairDropServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_onConnection(peer) {
|
_onConnection(peer) {
|
||||||
this._joinRoom(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._joinRoom(peer);
|
||||||
|
|
||||||
// send displayName
|
// send displayName
|
||||||
this._send(peer, {
|
this._send(peer, {
|
||||||
|
@ -317,6 +317,10 @@ class PairDropServer {
|
||||||
_joinRoom(peer, roomType = 'ip', roomSecret = '') {
|
_joinRoom(peer, roomType = 'ip', roomSecret = '') {
|
||||||
const room = roomType === 'ip' ? peer.ip : roomSecret;
|
const room = roomType === 'ip' ? peer.ip : roomSecret;
|
||||||
|
|
||||||
|
if (this._rooms[room] && this._rooms[room][peer.id]) {
|
||||||
|
this._leaveRoom(peer, roomType, roomSecret);
|
||||||
|
}
|
||||||
|
|
||||||
// if room doesn't exist, create it
|
// if room doesn't exist, create it
|
||||||
if (!this._rooms[room]) {
|
if (!this._rooms[room]) {
|
||||||
this._rooms[room] = {};
|
this._rooms[room] = {};
|
||||||
|
@ -341,10 +345,6 @@ class PairDropServer {
|
||||||
// delete the peer
|
// delete the peer
|
||||||
delete this._rooms[room][peer.id];
|
delete this._rooms[room][peer.id];
|
||||||
|
|
||||||
if (roomType === 'ip') {
|
|
||||||
peer.socket.terminate();
|
|
||||||
}
|
|
||||||
|
|
||||||
//if room is empty, delete the room
|
//if room is empty, delete the room
|
||||||
if (!Object.keys(this._rooms[room]).length) {
|
if (!Object.keys(this._rooms[room]).length) {
|
||||||
delete this._rooms[room];
|
delete this._rooms[room];
|
||||||
|
|
Loading…
Reference in a new issue