Automatically reoopen conversations on page reload

This commit is contained in:
2020-04-10 13:51:36 +02:00
parent 119a6f1626
commit c3226a4fe1
7 changed files with 178 additions and 6 deletions

View File

@ -0,0 +1,45 @@
/**
* Base class for events on custom class management
*
* @author Pierre Hubert
*/
class CustomEvents {
constructor() {
/**
* @type {Map<string, Array<(e) => any>>}
*/
this.evts = new Map();
}
/**
* Register to an event
*
* @param {string} evt The name of the event to register to
* @param {(e) => any} cb Callback function
*/
on(evt, cb) {
if(!this.evts.has(evt))
this.evts.set(evt, []);
this.evts.get(evt).push(cb)
}
/**
* Propagate a new event
*
* @param {string} evt The name of the event
* @param {any} data Data associated with the event
*/
emitEvent(evt, data) {
if(!this.evts.has(evt))
this.evts.set(evt, []);
this.evts.get(evt).forEach((e) => {
e(data)
})
}
}

View File

@ -42,7 +42,10 @@ class UserWebSocket {
this.ws = new WebSocket(url);
// Wait for connection
this.ws.addEventListener("open", () => console.log("Connected to websocket!"))
this.ws.addEventListener("open", () => {
console.info("Connected to websocket!");
SendEvent("wsOpen")
})
this.ws.addEventListener("error", (e) => this.Error(e))
this.ws.addEventListener("close", (e) => this.Closed(e));