always cleanup old sockets regardless of autoconnect

This commit is contained in:
Morgan 'ARR\!' Allen 2023-10-20 11:50:56 -07:00
parent a6feb49eb0
commit 903ec31b47
1 changed files with 9 additions and 9 deletions

View File

@ -16,13 +16,7 @@ class WSElement extends HTMLElement {
this.#connect_socket(false); this.#connect_socket(false);
} }
#connect_socket(recreate) { #connect_socket() {
// `recreate` is used to handle reconnects, removeing the old websocket object
// and recreating a new one
if(recreate && WSElement.#sockets[this.src]) {
delete WSElement.#sockets[this.src];
}
// a single WebSocket can be reused for multiple elements // a single WebSocket can be reused for multiple elements
// check to see if one exists for this `src` else create // check to see if one exists for this `src` else create
// a new one. // a new one.
@ -77,9 +71,11 @@ class WSElement extends HTMLElement {
this.close(sock); this.close(sock);
} }
delete WSElement.#sockets[this.src];
// handle autoreconnect and delay throttling // handle autoreconnect and delay throttling
if(this.autoreconnect) { if(this.autoreconnect) {
setTimeout(this.#connect_socket.call(this, true), this.#delay); setTimeout(this.#connect_socket.bind(this), this.#delay);
if(this.#delay < 10000) { if(this.#delay < 10000) {
this.#delay += 1000; this.#delay += 1000;
} }
@ -131,7 +127,11 @@ class WSInput extends WSSenderElement {
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
this.insertAdjacentHTML('beforeend', '<label class="time-label" for="ws-input-send">Send: </label> <input placeholder="Press enter" id="ws-input-send" />'); this.insertAdjacentHTML('beforeend', `
<label class="time-label" for="ws-input-send">Send: </label>
<input placeholder="Press enter" id="ws-input-send" />`
);
this.input = this.querySelector('input'); this.input = this.querySelector('input');
var self = this; var self = this;
this.input.addEventListener('keyup', function(evt) { this.input.addEventListener('keyup', function(evt) {