From 4cbb49e71a8a2c5d807f3ab98df924a8a08c5dc4 Mon Sep 17 00:00:00 2001 From: Christian Torbidone Date: Tue, 6 Aug 2024 20:22:05 +0200 Subject: [PATCH] Fixed WebSockets --- package.json | 4 +++- src/index.ts | 31 ++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index fb33c70..79debba 100644 --- a/package.json +++ b/package.json @@ -12,11 +12,13 @@ "test": "yarn build && node build/test.js" }, "dependencies": { + "@types/ws": "^8.5.12", "axios": "^1.6.2", "global": "^4.4.0", "socket.io-client": "^4.7.2", "ts-node": "^10.9.2", - "typescript": "^5.5.4" + "typescript": "^5.5.4", + "ws": "^8.18.0" }, "devDependencies": { "@types/node": "^20.10.3" diff --git a/src/index.ts b/src/index.ts index a26eecb..11d07e0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,9 @@ import CreditsManager from "./CreditsManager.js"; import EventEmitter from 'node:events'; import {IAManager} from "./IAManager.js"; +import { WebSocket } from "ws"; + +export const validateStatus = status => true export default class TNC extends EventEmitter { token: string | undefined; @@ -9,7 +12,6 @@ export default class TNC extends EventEmitter { IA: IAManager; id: string | undefined; socket: WebSocket | undefined; - privateSocket: WebSocket | undefined; /** * Constructor for the class. @@ -33,29 +35,24 @@ export default class TNC extends EventEmitter { * * @return {void} This function does not return any value. */ - connect(privateBoolean: boolean = false, secret: string | undefined = undefined) { - let link = "wss://api.thundernetwork.org/ws"; - if (privateBoolean && secret) { - link = "wss://api.thundernetwork.org/private"; - console.log("Connecting to private endpoint"); - } else { - console.log("Connecting to websocket"); - } + connect(options?: { + secret?: string, + customEndpoint?: string, + }) { + let link = options.customEndpoint ?? "wss://api.thundernetwork.org/ws"; + + console.log("Connecting to websocket"); const socket = new WebSocket(link); socket.addEventListener("open", () => { console.log("Connected to server"); - if (privateBoolean && secret) { - this.privateSocket = socket; - console.log("Authenticating"); - socket.send(secret); - } else { - this.socket = socket; - } + console.log("Authenticating"); + socket.send("auth " + (options.secret ?? this.token)); + this.socket = socket; }); socket.addEventListener("message", (event) => { - console.log("Message from server ", event.data); + console.log("Message from server", typeof event.data, event.data); }); } } \ No newline at end of file