add option to enable logger

This commit is contained in:
Janis
2023-08-03 00:12:57 +02:00
parent e920aa6f18
commit 7362d80a9e
8 changed files with 92 additions and 53 deletions

View File

@@ -1,7 +1,7 @@
import { IAuthSenderPayload, IChannel, IClient, IConnection, ITS5ConnectionHandler, ITS5DataHandler, ITS5MessageHandler } from "../../interfaces/teamspeak";
import { TS5DataHandler } from "./dataHandler";
import { TS5MessageHandler } from "./messageHandler";
import Logger from "../../utils/logger";
import { ILogger } from "../../utils/logger";
import { ITSRemoteAppAuthPayloadOptions } from "../../interfaces/api";
@@ -11,6 +11,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
ws: WebSocket; // Websocket connection to TS5 client
authenticated = false; // Is the connection authenticated?
remoteAppPort: number; // Port of TS5 client
logger: ILogger; // Logger
authPayload: ITSRemoteAppAuthPayloadOptions; // Authentication payload
dataHandler: ITS5DataHandler; // Handles data/lists and states
messageHandler: ITS5MessageHandler; // Handles messages received from TS5 client
@@ -19,6 +20,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
// Port of TS5 client
remoteAppPort: number,
authPayload: ITSRemoteAppAuthPayloadOptions,
logger: ILogger,
// State setters for dataHandler
setConnections: React.Dispatch<React.SetStateAction<IConnection[]>>,
setChannels: React.Dispatch<React.SetStateAction<IChannel[]>>,
@@ -30,15 +32,16 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
// Create websocket connection to TS5 client
this.remoteAppPort = remoteAppPort;
this.authPayload = authPayload;
this.logger = logger;
this.ws = new WebSocket(`ws://localhost:${this.remoteAppPort}`);
// Create dataHandler and messageHandler
this.dataHandler = new TS5DataHandler(setConnections, setChannels, setClients);
this.messageHandler = new TS5MessageHandler(this.ws, this.dataHandler, setActiveConnectionStateId);
this.dataHandler = new TS5DataHandler(setConnections, setChannels, setClients, logger);
this.messageHandler = new TS5MessageHandler(this.ws, this.dataHandler, setActiveConnectionStateId, logger);
}
reconnect() {
Logger.log("Reconnecting...")
this.logger.log("Reconnecting...")
this.ws.close();
this.ws = new WebSocket(`ws://localhost:${this.remoteAppPort}`);
@@ -50,7 +53,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
// Connect to TS5 client
connect() {
Logger.log('Connecting to TS5 client...');
this.logger.log('Connecting to TS5 client...');
// Create authentication payload
const initalPayload: IAuthSenderPayload = {
@@ -66,16 +69,16 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
this.ws.onopen = () => {
// Send authentication payload to TS5 client
this.ws.send(JSON.stringify(initalPayload));
Logger.wsSent(initalPayload);
this.logger.wsSent(initalPayload);
};
this.ws.onclose = (event) => {
Logger.log("WebSocket connection closed", event);
this.logger.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) {
Logger.log("WebSocket connection closed before authentication");
this.logger.log("WebSocket connection closed before authentication");
localStorage.removeItem("apiKey");
}
@@ -89,7 +92,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
this.ws.onmessage = (event) => {
const data = JSON.parse(event.data);
Logger.wsReceived(data)
this.logger.wsReceived(data)
switch (data.type) {
case "auth":
@@ -118,7 +121,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
this.messageHandler.handleChannelsMessage(data);
break;
default:
Logger.log(`No handler for event type: ${data.type}`);
this.logger.log(`No handler for event type: ${data.type}`);
break;
}
};