Update connection method in JS player so it can be extended

This commit is contained in:
Alexey Khit
2022-12-09 00:24:29 +03:00
parent c9dd0e37e4
commit 9f6af1c9e4
+25 -21
View File
@@ -194,7 +194,7 @@ class VideoRTC extends HTMLElement {
this.wsState = WebSocket.CONNECTING; this.wsState = WebSocket.CONNECTING;
this.internalInit(); this.internalInit();
this.internalWS(); this.internalConnect();
} }
/** /**
@@ -272,7 +272,7 @@ class VideoRTC extends HTMLElement {
} }
} }
internalWS() { internalConnect() {
if (this.wsState !== WebSocket.CONNECTING) return; if (this.wsState !== WebSocket.CONNECTING) return;
if (this.ws) throw "connect with non null WebSocket"; if (this.ws) throw "connect with non null WebSocket";
@@ -287,6 +287,29 @@ class VideoRTC extends HTMLElement {
// CONNECTING => OPEN // CONNECTING => OPEN
this.wsState = WebSocket.OPEN; this.wsState = WebSocket.OPEN;
this.internalOpen();
});
this.ws.addEventListener("close", () => {
console.debug("VideoRTC.ws.close", this.wsState);
if (this.wsState === WebSocket.CLOSED) return;
// CONNECTING, OPEN => CONNECTING
this.wsState = WebSocket.CONNECTING;
this.ws = null;
// reconnect no more than once every X seconds
const delay = Math.max(this.RECONNECT_TIMEOUT - (Date.now() - ts), 0);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = 0;
this.internalConnect();
}, delay);
});
}
internalOpen() {
this.ws.addEventListener("message", ev => { this.ws.addEventListener("message", ev => {
if (typeof ev.data === "string") { if (typeof ev.data === "string") {
const msg = JSON.parse(ev.data); const msg = JSON.parse(ev.data);
@@ -326,25 +349,6 @@ class VideoRTC extends HTMLElement {
this.internalMJPEG(); this.internalMJPEG();
} }
} }
});
this.ws.addEventListener("close", () => {
console.debug("VideoRTC.ws.close", this.wsState);
if (this.wsState === WebSocket.CLOSED) return;
// CONNECTING, OPEN => CONNECTING
this.wsState = WebSocket.CONNECTING;
this.ws = null;
// reconnect no more than once every X seconds
const delay = Math.max(this.RECONNECT_TIMEOUT - (Date.now() - ts), 0);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = 0;
this.internalWS();
}, delay);
});
} }
internalMSE() { internalMSE() {