[refactor] add reconnect

This commit is contained in:
Janis
2023-06-28 17:23:23 +02:00
parent 9162aede1a
commit c5c76ea39a

View File

@@ -5,6 +5,8 @@ import { IAuthMessage, IAuthSenderPayload, IChannel, IChannelInfos, IChannelsMes
// Main class // Main class
export class TS5Connection { export class TS5Connection {
ws: WebSocket; // Websocket connection to TS5 client ws: WebSocket; // Websocket connection to TS5 client
authenticated = false; // Is the connection authenticated?
remoteAppPort: number; // Port of TS5 client
dataHandler: TS5DataHandler; // Handles data/lists and states dataHandler: TS5DataHandler; // Handles data/lists and states
messageHandler: TS5MessageHandler; // Handles messages received from TS5 client messageHandler: TS5MessageHandler; // Handles messages received from TS5 client
@@ -19,17 +21,28 @@ export class TS5Connection {
setActiveConnectionId: React.Dispatch<React.SetStateAction<number>>, setActiveConnectionId: React.Dispatch<React.SetStateAction<number>>,
) { ) {
// Create websocket connection to TS5 client // Create websocket connection to TS5 client
this.ws = new WebSocket(`ws://localhost:${remoteAppPort}`); this.remoteAppPort = remoteAppPort;
this.ws = new WebSocket(`ws://localhost:${this.remoteAppPort}`);
// Create dataHandler and messageHandler // Create dataHandler and messageHandler
this.dataHandler = new TS5DataHandler(setConnections, setChannels, setClients); this.dataHandler = new TS5DataHandler(setConnections, setChannels, setClients);
this.messageHandler = new TS5MessageHandler(this.ws, this.dataHandler, setActiveConnectionId); this.messageHandler = new TS5MessageHandler(this.ws, this.dataHandler, setActiveConnectionId);
} }
reconnect() {
this.ws.close();
this.ws = new WebSocket(`ws://localhost:${this.remoteAppPort}`);
this.dataHandler.clearAll();
this.authenticated = false;
this.connect();
}
// Connect to TS5 client // Connect to TS5 client
connect() { connect() {
console.log('Connecting to TS5 client...'); console.log('Connecting to TS5 client...');
console.log(localStorage.getItem("apiKey"))
// Create authentication payload // Create authentication payload
const initalPayload: IAuthSenderPayload = { const initalPayload: IAuthSenderPayload = {
@@ -53,6 +66,15 @@ export class TS5Connection {
this.ws.onclose = (event) => { this.ws.onclose = (event) => {
console.log("WebSocket connection closed", event); console.log("WebSocket connection closed", event);
// If the connection was closed before authentication, remove the API key from local storage
// OBS weirdly caches the localstorage and is very stubborn about clearing it (even when clicken "Clear Cache")
if (!this.authenticated) {
console.log("WebSocket connection closed before authentication");
localStorage.removeItem("apiKey");
}
this.reconnect();
}; };
// Handle messages received from TS5 client // Handle messages received from TS5 client
@@ -65,6 +87,7 @@ export class TS5Connection {
switch (data.type) { switch (data.type) {
case "auth": case "auth":
this.messageHandler.handleAuthMessage(data); this.messageHandler.handleAuthMessage(data);
this.authenticated = true;
break; break;
case "clientMoved": case "clientMoved":
this.messageHandler.handleClientMovedMessage(data); this.messageHandler.handleClientMovedMessage(data);
@@ -136,6 +159,17 @@ class TS5DataHandler {
this.setClients([...this.localClients]); this.setClients([...this.localClients]);
} }
// Clear all data
clearAll() {
this.localConnections = [];
this.localChannels = [];
this.localClients = [];
this.updateConnectionsState();
this.updateChannelsState();
this.updateClientsState();
}
// Add data to local lists and update states // Add data to local lists and update states
addConnection(connection: IConnection) { addConnection(connection: IConnection) {
console.log("Adding connection...", connection) console.log("Adding connection...", connection)