Question:
I have a Socket.io project that, when I send a command (through console), it sends “serverstop” and the users on the website refresh. What happens, though, is that the user refreshes again and again until I restart the project (so chatStopped is false). How can I make them refresh ONCE and then it functions like normal?
Repl link:
https://replit.com/@doxr/TalkRN#index.js
I hope it’s OK that I’m posting a lot of code, I shorted it for this case.
// Check if chat is stopped
let chatStopped = false;
// How I check for the command in this case
process.stdin.on('data', (data) => {
const command = data.toString().trim();
if (command === 'serverstop') {
chatStopped = true;
console.log('Chat stopped');
} else {
console.log('Command not Recognized.')
}
});
// Some more code and then it goes to this
io.on('connection', (socket) => {
socket.on('chat message', (msg) => {
if (!chatStopped) {
io.emit('chat message', md.render(msg));
} else {
io.emit('serverstop');
}
});
});
and this is for the browser
// So what happens is that when this serverstop is received, the site basically redirects to itself which is how simulate refreshing
socket.on('serverstop', function () {
window.location.replace("https://talkrn.doxr.repl.co");
});
but the user refreshes again and again, I thought it would happen once. I would appreciate some help!