Answer the question
In order to leave comments, you need to log in
Websocket reconnect slows down in Firefox. Why is that? How to overcome?
Hello colleagues!
I implement communication with the server using WebSockets.
According to the plan, there should be an automatic reconnect.
I do it with code like this:
"use strict";
let webSocket = undefined;
let timer = 0;
function Link (url) {
function startWebSocket () {
if (webSocket !== undefined) return;
webSocket = new WebSocket(url);
webSocket.onmessage = event => console.log(event);
webSocket.onopen = () => {
if (timer === 0) return;
clearInterval(timer);
timer = 0;
};
webSocket.onclose = () => {
webSocket = undefined;
if (timer === 0) timer = setInterval(startWebSocket, 1000);
};
}
function sendMessage (message) {
if (webSocket === undefined) return;
webSocket.send(message);
}
return {
send: sendMessage,
start: startWebSocket
};
}
export default Link;
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