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" "test": "yarn build && node build/test.js"
}, },
"dependencies": { "dependencies": {
"@types/ws": "^8.5.12",
"axios": "^1.6.2", "axios": "^1.6.2",
"global": "^4.4.0", "global": "^4.4.0",
"socket.io-client": "^4.7.2", "socket.io-client": "^4.7.2",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"typescript": "^5.5.4" "typescript": "^5.5.4",
"ws": "^8.18.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.10.3" "@types/node": "^20.10.3"

View file

@ -1,6 +1,9 @@
import CreditsManager from "./CreditsManager.js"; import CreditsManager from "./CreditsManager.js";
import EventEmitter from 'node:events'; import EventEmitter from 'node:events';
import {IAManager} from "./IAManager.js"; import {IAManager} from "./IAManager.js";
import { WebSocket } from "ws";
export const validateStatus = status => true
export default class TNC extends EventEmitter { export default class TNC extends EventEmitter {
token: string | undefined; token: string | undefined;
@ -9,7 +12,6 @@ export default class TNC extends EventEmitter {
IA: IAManager; IA: IAManager;
id: string | undefined; id: string | undefined;
socket: WebSocket | undefined; socket: WebSocket | undefined;
privateSocket: WebSocket | undefined;
/** /**
* Constructor for the class. * Constructor for the class.
@ -33,29 +35,24 @@ export default class TNC extends EventEmitter {
* *
* @return {void} This function does not return any value. * @return {void} This function does not return any value.
*/ */
connect(privateBoolean: boolean = false, secret: string | undefined = undefined) { connect(options?: {
let link = "wss://api.thundernetwork.org/ws"; secret?: string,
if (privateBoolean && secret) { customEndpoint?: string,
link = "wss://api.thundernetwork.org/private"; }) {
console.log("Connecting to private endpoint"); let link = options.customEndpoint ?? "wss://api.thundernetwork.org/ws";
} else {
console.log("Connecting to websocket"); console.log("Connecting to websocket");
}
const socket = new WebSocket(link); const socket = new WebSocket(link);
socket.addEventListener("open", () => { socket.addEventListener("open", () => {
console.log("Connected to server"); console.log("Connected to server");
if (privateBoolean && secret) {
this.privateSocket = socket;
console.log("Authenticating"); console.log("Authenticating");
socket.send(secret); socket.send("auth " + (options.secret ?? this.token));
} else {
this.socket = socket; this.socket = socket;
}
}); });
socket.addEventListener("message", (event) => { socket.addEventListener("message", (event) => {
console.log("Message from server ", event.data); console.log("Message from server", typeof event.data, event.data);
}); });
} }
} }