add proper error and disconnect handling

error handling shows you the error and offers the ability to reload;
disconnect handling shows you that the page will reload in a few seconds.
it uses exponential backoff with some random sprinkled into it to prevent overwhelming the server once people's clients decide to reconnect.
This commit is contained in:
りき萌 2024-08-25 12:53:53 +02:00
parent 84abba3e0b
commit 0d831698e2
8 changed files with 150 additions and 20 deletions

View file

@ -134,6 +134,11 @@ class Session extends EventTarget {
}
});
this.ws.addEventListener("close", (_) => {
console.info("connection closed");
this.dispatchEvent(new Event("disconnect"));
});
try {
await listen([this.ws, "open"]);
await this.joinInner(wallId, userInit);
@ -269,8 +274,10 @@ class Session extends EventTarget {
}
}
export async function newSession(userId, secret, wallId, userInit) {
export async function newSession({ userId, secret, wallId, userInit, onError, onDisconnect }) {
let session = new Session(userId, secret);
session.addEventListener("error", onError);
session.addEventListener("disconnect", onDisconnect);
await session.join(wallId, userInit);
return session;
}