Merge branch 'feat-lobby' of github.com:havfo/multiparty-meeting into feat-lobby

master
Håvar Aambø Fosstveit 2019-10-24 00:01:33 +02:00
commit 77bbc53124
3 changed files with 67 additions and 21 deletions

View File

@ -32,6 +32,14 @@ class Lobby extends EventEmitter
this._peers = {}; this._peers = {};
} }
checkEmpty()
{
logger.info('checkEmpty()');
if (Object.keys(this._peers).length == 0)
return true
else return false;
}
peerList() peerList()
{ {
logger.info('peerList()'); logger.info('peerList()');
@ -77,6 +85,8 @@ class Lobby extends EventEmitter
{ {
logger.info('parkPeer()'); logger.info('parkPeer()');
if ( this._closed ) return;
const peer = { peerId, socket, consume }; const peer = { peerId, socket, consume };
socket.emit('notification', { method: 'enteredLobby', data: {} }); socket.emit('notification', { method: 'enteredLobby', data: {} });
@ -88,7 +98,8 @@ class Lobby extends EventEmitter
logger.debug( logger.debug(
'Peer "request" event [method:%s, peerId:%s]', 'Peer "request" event [method:%s, peerId:%s]',
request.method, peer.peerId); request.method, peer.peerId);
if (this._closed) return;
this._handleSocketRequest(peer, request, cb) this._handleSocketRequest(peer, request, cb)
.catch((error) => .catch((error) =>
{ {
@ -100,19 +111,22 @@ class Lobby extends EventEmitter
socket.on('disconnect', () => socket.on('disconnect', () =>
{ {
if (this._closed)
return;
logger.debug('Peer "close" event [peerId:%s]', peer.peerId); logger.debug('Peer "close" event [peerId:%s]', peer.peerId);
if (this._closed) return;
this.emit('peerClosed', peer); this.emit('peerClosed', peer);
delete this._peers[peer.peerId]; delete this._peers[peer.peerId];
if ( this.checkEmpty() ) this.emit('lobbyEmpty');
}); });
} }
async _handleSocketRequest(peer, request, cb) async _handleSocketRequest(peer, request, cb)
{ {
logger.debug('_handleSocketRequest [peer:%o], [request:%o]', peer, request);
if (this._closed) return;
switch (request.method) switch (request.method)
{ {
case 'changeDisplayName': case 'changeDisplayName':

View File

@ -98,6 +98,16 @@ class Room extends EventEmitter
}); });
}); });
// If nobody left in lobby we should check if room is empty too and initiating
// rooms selfdestruction sequence
this._lobby.on('lobbyEmpty', () =>
{
if ( this.checkEmpty() )
{
this.selfDestructCountdown();
}
})
this._chatHistory = []; this._chatHistory = [];
this._fileHistory = []; this._fileHistory = [];
@ -154,6 +164,25 @@ class Room extends EventEmitter
return this._roomId; return this._roomId;
} }
selfDestructCountdown()
{
logger.debug('selfDestructCountdown() started')
setTimeout(() =>
{
if (this._closed)
return;
if (this.checkEmpty() && this._lobby.checkEmpty())
{
logger.info(
'Room deserted for some time, closing the room [roomId:%s]',
this._roomId);
this.close();
}
else logger.debug('selfDestructCountdown() aborted; room is not empty!')
}, 10000);
}
close() close()
{ {
logger.debug('close()'); logger.debug('close()');
@ -186,6 +215,13 @@ class Room extends EventEmitter
); );
} }
// checks both room and lobby
checkEmpty()
{
if (( Object.keys(this._peers).length == 0) && (this._lobby.checkEmpty())) return true
else return false;
}
handleConnection({ peerId, consume, socket }) handleConnection({ peerId, consume, socket })
{ {
logger.info('handleConnection() [peerId:"%s"]', peerId); logger.info('handleConnection() [peerId:"%s"]', peerId);
@ -295,7 +331,7 @@ class Room extends EventEmitter
if (this._closed) if (this._closed)
return; return;
logger.debug('Peer "close" event [peerId:%s]', peer.id); logger.debug('Peer "disconnect" event [peerId:%s]', peer.id);
// If the Peer was joined, notify all Peers. // If the Peer was joined, notify all Peers.
if (peer.data.joined) if (peer.data.joined)
@ -319,23 +355,10 @@ class Room extends EventEmitter
delete this._peers[peer.id]; delete this._peers[peer.id];
// If this is the latest Peer in the room, close the room after a while. // If this is the latest Peer in the room and lobby is empty, close the room after a while.
if (Object.keys(this._peers).length == 0) if (this.checkEmpty())
{ {
setTimeout(() => this.selfDestructCountdown();
{
if (this._closed)
return;
if (Object.keys(this._peers).length == 0)
{
logger.info(
'last Peer in the room left, closing the room [roomId:%s]',
this._roomId);
this.close();
}
}, 10000);
} }
}); });
} }

View File

@ -136,6 +136,15 @@ async function run()
room.logStatus(); room.logStatus();
} }
}, 120000); }, 120000);
// check for deserted rooms
setInterval(() =>
{
for (const room of rooms.values())
{
room.checkEmpty();
}
}, 10000);
} }
async function setupAuth(oidcIssuer) async function setupAuth(oidcIssuer)