Update connection method in JS player so it can be extended
This commit is contained in:
+25
-21
@@ -194,7 +194,7 @@ class VideoRTC extends HTMLElement {
|
||||
this.wsState = WebSocket.CONNECTING;
|
||||
|
||||
this.internalInit();
|
||||
this.internalWS();
|
||||
this.internalConnect();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -272,7 +272,7 @@ class VideoRTC extends HTMLElement {
|
||||
}
|
||||
}
|
||||
|
||||
internalWS() {
|
||||
internalConnect() {
|
||||
if (this.wsState !== WebSocket.CONNECTING) return;
|
||||
if (this.ws) throw "connect with non null WebSocket";
|
||||
|
||||
@@ -287,6 +287,29 @@ class VideoRTC extends HTMLElement {
|
||||
// CONNECTING => 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 => {
|
||||
if (typeof ev.data === "string") {
|
||||
const msg = JSON.parse(ev.data);
|
||||
@@ -326,25 +349,6 @@ class VideoRTC extends HTMLElement {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user