From be3698e76d9fe794f4a3319f5d9447de30973455 Mon Sep 17 00:00:00 2001 From: Holly Date: Tue, 22 Feb 2022 09:11:17 +0000 Subject: [PATCH] stream users don't need to be checked on interval, just do it on removal. --- server/Streams.js | 35 +++++++++++------------------------ server/serverHandler.js | 3 --- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/server/Streams.js b/server/Streams.js index daf2d68..8f5a174 100644 --- a/server/Streams.js +++ b/server/Streams.js @@ -21,26 +21,6 @@ module.exports = class { consoleHelper.printBancho(`Added stream [${streamName}]`); } - // Checks if a stream has no users in it - streamChecker(interval = 5000) { - setInterval(() => { - // Get the names of all currently avaliable streams - const streams = global.StreamsHandler.getStreams(); - // Loop through all streams - for (let i = 0; i < streams.length; i++) { - // Get the current stream - const currentStream = global.StreamsHandler.avaliableStreams[streams[i]]; - // Check if the stream should be removed if there are no users in it - // And if the stream has no users in it - if (currentStream.removeIfEmpty && currentStream.streamUsers.length == 0) { - global.StreamsHandler.removeStream(streams[i]); - consoleHelper.printBancho(`Removed stream [${streams[i]}] There were no users in stream`); - } - } - }, interval); - consoleHelper.printBancho(`BinatoStream is running! Checks running at a ${interval}ms interval`); - } - removeStream(streamName) { try { delete this.avaliableStreams[streamName]; @@ -74,8 +54,10 @@ module.exports = class { if (!this.doesStreamExist(streamName)) return consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because it does not exist!`); + const stream = this.avaliableStreams[streamName]; + // Make sure the user isn't already in the stream - if (!this.avaliableStreams[streamName].streamUsers.includes(userToken)) + if (!stream.streamUsers.includes(userToken)) return consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because they are not in it!`); // Make sure this isn't an invalid user (userId can't be lower than 1) @@ -84,20 +66,25 @@ module.exports = class { try { // Find index of user to remove let userCurrentIndex; - for (let i = 0; i < this.avaliableStreams[streamName].streamUsers.length; i++) { - if (userToken == this.avaliableStreams[streamName].streamUsers[i]) { + for (let i = 0; i < stream.streamUsers.length; i++) { + if (userToken == stream.streamUsers[i]) { userCurrentIndex = i; break; } } // Remove user from stream's user list - this.avaliableStreams[streamName].streamUsers.splice(userCurrentIndex, 1); + stream.streamUsers.splice(userCurrentIndex, 1); consoleHelper.printBancho(`Removed user [${userToken}] from stream ${streamName}`); } catch (e) { consoleHelper.printBancho(`Can't Remove user [${userToken}] from stream ${streamName}`); console.error(e); } + + if (stream.removeIfEmpty && stream.streamUsers.length == 0) { + this.removeStream(stream); + consoleHelper.printBancho(`Removed stream [${streamName}] There were no users in stream`); + } } sendToStream(streamName, streamData, initUser = null) { diff --git a/server/serverHandler.js b/server/serverHandler.js index 660fb06..1fa7b12 100644 --- a/server/serverHandler.js +++ b/server/serverHandler.js @@ -82,9 +82,6 @@ for (let i = 0; i < global.channels.length; i++) { // Add a stream for the multiplayer lobby global.StreamsHandler.addStream("multiplayer_lobby", false); -// Start stream checking interval -global.StreamsHandler.streamChecker(5000); - // Server stats global.usersOnline = 0; global.multiplayerMatches = [0, 0]