Fixed WebSockets

This commit is contained in:
Christian Torbidone 2024-08-06 20:22:05 +02:00
parent 3049877132
commit 4cbb49e71a
2 changed files with 17 additions and 18 deletions

View file

@ -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"

View file

@ -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);
});
}
}