stream users don't need to be checked on interval, just do it on removal.

This commit is contained in:
Holly Stubbs 2022-02-22 09:11:17 +00:00
parent 604e698f8e
commit be3698e76d
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 11 additions and 27 deletions

View file

@ -21,26 +21,6 @@ module.exports = class {
consoleHelper.printBancho(`Added stream [${streamName}]`); 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) { removeStream(streamName) {
try { try {
delete this.avaliableStreams[streamName]; delete this.avaliableStreams[streamName];
@ -74,8 +54,10 @@ module.exports = class {
if (!this.doesStreamExist(streamName)) if (!this.doesStreamExist(streamName))
return consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because it does not exist!`); 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 // 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!`); 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) // Make sure this isn't an invalid user (userId can't be lower than 1)
@ -84,20 +66,25 @@ module.exports = class {
try { try {
// Find index of user to remove // Find index of user to remove
let userCurrentIndex; let userCurrentIndex;
for (let i = 0; i < this.avaliableStreams[streamName].streamUsers.length; i++) { for (let i = 0; i < stream.streamUsers.length; i++) {
if (userToken == this.avaliableStreams[streamName].streamUsers[i]) { if (userToken == stream.streamUsers[i]) {
userCurrentIndex = i; userCurrentIndex = i;
break; break;
} }
} }
// Remove user from stream's user list // 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}`); consoleHelper.printBancho(`Removed user [${userToken}] from stream ${streamName}`);
} catch (e) { } catch (e) {
consoleHelper.printBancho(`Can't Remove user [${userToken}] from stream ${streamName}`); consoleHelper.printBancho(`Can't Remove user [${userToken}] from stream ${streamName}`);
console.error(e); 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) { sendToStream(streamName, streamData, initUser = null) {

View file

@ -82,9 +82,6 @@ for (let i = 0; i < global.channels.length; i++) {
// Add a stream for the multiplayer lobby // Add a stream for the multiplayer lobby
global.StreamsHandler.addStream("multiplayer_lobby", false); global.StreamsHandler.addStream("multiplayer_lobby", false);
// Start stream checking interval
global.StreamsHandler.streamChecker(5000);
// Server stats // Server stats
global.usersOnline = 0; global.usersOnline = 0;
global.multiplayerMatches = [0, 0] global.multiplayerMatches = [0, 0]