refactor(WebSocketConnection): simplify connection logic by removing demo mode checks and streamlining connection process
This commit is contained in:
@@ -3,34 +3,23 @@ import mitt from "mitt";
|
|||||||
class WebSocketConnection {
|
class WebSocketConnection {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.emitter = mitt();
|
this.emitter = mitt();
|
||||||
this.isDemoMode = false;
|
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
this.pingInterval = null;
|
this.pingInterval = null;
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
this.checkDemoModeAndConnect();
|
this.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkDemoModeAndConnect() {
|
async connect() {
|
||||||
if (typeof window === "undefined" || !window.axios) {
|
if (typeof window === "undefined" || !window.axios) {
|
||||||
setTimeout(() => this.checkDemoModeAndConnect(), 100);
|
setTimeout(() => this.connect(), 100);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await window.axios.get("/api/v1/app/info");
|
|
||||||
this.isDemoMode = response.data.app_info?.is_demo === true;
|
|
||||||
} catch {
|
|
||||||
// If we can't check, assume not demo mode and try to connect
|
|
||||||
}
|
|
||||||
|
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
|
this.reconnect();
|
||||||
if (!this.isDemoMode) {
|
this.pingInterval = setInterval(() => {
|
||||||
this.reconnect();
|
this.ping();
|
||||||
this.pingInterval = setInterval(() => {
|
}, 30000);
|
||||||
this.ping();
|
|
||||||
}, 30000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add event listener
|
// add event listener
|
||||||
@@ -49,7 +38,7 @@ class WebSocketConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reconnect() {
|
reconnect() {
|
||||||
if (!this.initialized || this.isDemoMode) {
|
if (!this.initialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,11 +48,9 @@ class WebSocketConnection {
|
|||||||
|
|
||||||
// auto reconnect when websocket closes
|
// auto reconnect when websocket closes
|
||||||
this.ws.addEventListener("close", () => {
|
this.ws.addEventListener("close", () => {
|
||||||
if (!this.isDemoMode) {
|
setTimeout(() => {
|
||||||
setTimeout(() => {
|
this.reconnect();
|
||||||
this.reconnect();
|
}, 1000);
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// emit data received from websocket
|
// emit data received from websocket
|
||||||
@@ -79,9 +66,6 @@ class WebSocketConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ping() {
|
ping() {
|
||||||
if (this.isDemoMode) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
this.send(
|
this.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|||||||
Reference in New Issue
Block a user