Answer the question
In order to leave comments, you need to log in
What is the right way to put an event handler under socket.io?
I connect to the websocket server as a socket.io client, while simultaneously receiving information from another server (via pusher), which I first parse, then send this information via a websocket connection (via socket.emit). At the same time, I can send 1 request per second, but I can, for example, 20, because of this - an error occurs in the websocket that there are a lot of requests. I did this - I connected to 2 handlers, the information I needed and errors. I add the necessary information from another server (pusher) to the array, from there I send it to the websocket (socket.emit). If I get an error, I wait and send it again, if I get no error, I remove the element from the array and check further. At the same time, you need to check the array in order to understand whether it is empty or not, because of this I call 1 more function, which I check every 5ms (this is important for me, the same 100ms - it will be a long time). Hence the question - is it possible to do something more correctly and how much does it generally affect the whole situation if the processor loads half a percent?
let array = []
let start = true
setInterval(checkSocket, 5)
function checkSocket() {
if (array.length > 0 && start == true) {
start = false
socket.emit('123')
}
}
socket.on('joined', () => {
console.log('подключился')
});
socket.on('error', function incoming(err) {
setTimeout(() => socket.emit('123'), 200);
})
socket.on('message', function incoming(mes) {
console.log(mes)
array.shift()
start = true
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question