fix last player leaving and not getting removed from session

This commit is contained in:
Steffen 2020-05-27 22:55:33 +02:00
parent 8a43c1e3e3
commit bcbbde1f30
No known key found for this signature in database
GPG Key ID: 764D74E98267DFC6
1 changed files with 8 additions and 5 deletions

View File

@ -64,6 +64,7 @@ class LiveSession {
*/ */
_ping() { _ping() {
this._send("ping", [this._isSpectator, this._store.state.session.playerId]); this._send("ping", [this._isSpectator, this._store.state.session.playerId]);
this._handlePing();
clearTimeout(this._pingTimer); clearTimeout(this._pingTimer);
this._pingTimer = setTimeout(this._ping.bind(this), this._pingInterval); this._pingTimer = setTimeout(this._ping.bind(this), this._pingInterval);
} }
@ -283,9 +284,14 @@ class LiveSession {
* @param playerId * @param playerId
* @private * @private
*/ */
_handlePing([isSpectator, playerId]) { _handlePing([isSpectator, playerId] = []) {
const now = new Date().getTime(); const now = new Date().getTime();
this._players[playerId] = now; if (playerId) {
this._players[playerId] = now;
if (!this._isSpectator && !isSpectator) {
alert("Another storyteller joined the session!");
}
}
// remove players that haven't sent a ping in twice the timespan // remove players that haven't sent a ping in twice the timespan
for (let player in this._players) { for (let player in this._players) {
if (now - this._players[player] > this._pingInterval * 2) { if (now - this._players[player] > this._pingInterval * 2) {
@ -293,9 +299,6 @@ class LiveSession {
} }
} }
this._store.commit("setPlayerCount", Object.keys(this._players).length); this._store.commit("setPlayerCount", Object.keys(this._players).length);
if (!this._isSpectator && !isSpectator) {
alert("Another storyteller joined the session!");
}
} }
/** /**