2022-06-19 00:25:21 +00:00
|
|
|
import {store} from "../store";
|
2019-11-16 17:24:03 +00:00
|
|
|
import socket from "../socket";
|
2019-11-05 19:22:10 +00:00
|
|
|
|
|
|
|
socket.on("disconnect", handleDisconnect);
|
|
|
|
socket.on("connect_error", handleDisconnect);
|
|
|
|
socket.on("error", handleDisconnect);
|
|
|
|
|
2020-11-25 16:02:48 +00:00
|
|
|
socket.io.on("reconnect_attempt", function (attempt) {
|
2019-11-05 19:22:10 +00:00
|
|
|
store.commit("currentUserVisibleError", `Reconnecting… (attempt ${attempt})`);
|
|
|
|
updateLoadingMessage();
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
socket.on("connecting", function () {
|
2019-11-20 18:09:21 +00:00
|
|
|
store.commit("currentUserVisibleError", "Connecting…");
|
2019-11-05 19:22:10 +00:00
|
|
|
updateLoadingMessage();
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
socket.on("connect", function () {
|
2019-11-05 19:22:10 +00:00
|
|
|
// Clear send buffer when reconnecting, socket.io would emit these
|
|
|
|
// immediately upon connection and it will have no effect, so we ensure
|
|
|
|
// nothing is sent to the server that might have happened.
|
|
|
|
socket.sendBuffer = [];
|
|
|
|
|
|
|
|
store.commit("currentUserVisibleError", "Finalizing connection…");
|
|
|
|
updateLoadingMessage();
|
|
|
|
});
|
|
|
|
|
|
|
|
function handleDisconnect(data) {
|
2022-06-19 00:25:21 +00:00
|
|
|
const message = String(data.message || data);
|
2019-11-05 19:22:10 +00:00
|
|
|
|
|
|
|
store.commit("isConnected", false);
|
2019-11-20 18:09:21 +00:00
|
|
|
|
|
|
|
if (!socket.io.reconnection()) {
|
|
|
|
store.commit(
|
|
|
|
"currentUserVisibleError",
|
|
|
|
`Disconnected from the server (${message}), The Lounge does not reconnect in public mode.`
|
|
|
|
);
|
|
|
|
updateLoadingMessage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:22:10 +00:00
|
|
|
store.commit("currentUserVisibleError", `Waiting to reconnect… (${message})`);
|
|
|
|
updateLoadingMessage();
|
|
|
|
|
|
|
|
// If the server shuts down, socket.io skips reconnection
|
|
|
|
// and we have to manually call connect to start the process
|
|
|
|
// However, do not reconnect if TL client manually closed the connection
|
2022-06-19 00:25:21 +00:00
|
|
|
// @ts-expect-error Property 'skipReconnect' is private and only accessible within class 'Manager<ListenEvents, EmitEvents>'.ts(2341)
|
2019-11-05 19:22:10 +00:00
|
|
|
if (socket.io.skipReconnect && message !== "io client disconnect") {
|
|
|
|
requestIdleCallback(() => socket.connect(), 2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestIdleCallback(callback, timeout) {
|
|
|
|
if (window.requestIdleCallback) {
|
|
|
|
// During an idle period the user agent will run idle callbacks in FIFO order
|
|
|
|
// until either the idle period ends or there are no more idle callbacks eligible to be run.
|
|
|
|
window.requestIdleCallback(callback, {timeout});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateLoadingMessage() {
|
|
|
|
const loading = document.getElementById("loading-page-message");
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
loading.textContent = store.state.currentUserVisibleError;
|
|
|
|
}
|
|
|
|
}
|