notify dev of connection end

This commit is contained in:
Rolands 2024-12-03 13:48:03 +01:00
parent 087a4f178d
commit 65976e70d6

View file

@ -73,10 +73,25 @@ export class SocioClient extends LogHandler {
async #connect(url: string, keep_alive: boolean, verbose: boolean, reconnect_tries:number){
this.#ws = new WebSocket(url);
this.#ws.addEventListener('message', this.#message.bind(this));
// on socket error events, keep retrying the connection until retries runs out
if (keep_alive && reconnect_tries){
this.#ws.addEventListener("close", (event: CloseEvent) => { this.#RetryConn(url, keep_alive, verbose, reconnect_tries, event) });
this.#ws.addEventListener("error", (event: Event) => { this.#RetryConn(url, keep_alive, verbose, reconnect_tries, event) });
}
// or notify everything that the connection has failed
else{
const notify = (event: Event | CloseEvent) => {
// log to console, as thats the first sign for the dev that smth is wrong
if (event instanceof CloseEvent) this.HandleInfo('Connection closed.');
else this.HandleError(new E(`Socio failed to connect [${url}]`, event));
// notify the hook, if it exists. retries would be 0 here, since the top if would've run it to 0
if (this.lifecycle_hooks.discon)
this.lifecycle_hooks.discon(this, url, keep_alive, verbose, reconnect_tries, event);
};
this.#ws.addEventListener("close", notify);
this.#ws.addEventListener("error", notify);
}
}
#RetryConn(url: string, keep_alive: boolean, verbose: boolean, reconnect_tries: number, event:any) {
this.HandleError(new E(`"${this.config.name || ''}" WebSocket closed. Retrying... Event details:`, event));