From 1f0b99012488b581c47082d6679353eed6925d89 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Mon, 9 Sep 2024 20:34:01 -0400 Subject: [PATCH] this should work? (pls) --- JS-SDK/.prettierrc | 4 +- JS-SDK/bun.lockb | Bin 64021 -> 64021 bytes JS-SDK/package.json | 82 +++++++++++++++++------------------ JS-SDK/src/hook/websocket.ts | 39 +++++++++++++++-- 4 files changed, 78 insertions(+), 47 deletions(-) diff --git a/JS-SDK/.prettierrc b/JS-SDK/.prettierrc index 2ef2d71..5f9f341 100644 --- a/JS-SDK/.prettierrc +++ b/JS-SDK/.prettierrc @@ -1,4 +1,4 @@ { - "trailingComma": "es5", - "tabWidth": 4 + "trailingComma": "es5", + "tabWidth": 4 } \ No newline at end of file diff --git a/JS-SDK/bun.lockb b/JS-SDK/bun.lockb index 59a3f6844dde0a4087d77ab3d5e6574f76ea1ad8..d7cc81cb77dae0d86af19c0f0b2694fdafb46999 100644 GIT binary patch delta 22 ecmbRGg?Z{1<_!nevopq7=o#smY(BBx*9!o1NC{d1 delta 22 acmbRGg?Z{1<_!nevokS(!R8a|eZ2r@atH$e diff --git a/JS-SDK/package.json b/JS-SDK/package.json index 47bd9d3..133e5f5 100644 --- a/JS-SDK/package.json +++ b/JS-SDK/package.json @@ -1,43 +1,43 @@ { - "name": "usetether", - "version": "1.1.3", - "author": "Braydon (Rainnny) ", - "description": "An API designed to provide real-time access to a user's Discord data.", - "keywords": [ - "java", - "discord", - "api", - "restful", - "realtime" - ], - "homepage": "https://usetether.rest", - "repository": { - "type": "git", - "url": "git+https://github.com/Rainnny7/Tether.git" - }, - "license": "MIT", - "main": "dist/index.js", - "types": "dist/index.d.ts", - "scripts": { - "build": "tsup", - "prepublishOnly": "bun run build" - }, - "files": [ - "dist" - ], - "devDependencies": { - "@types/bun": "latest", - "@types/react": "^18.3.5", - "@types/react-dom": "^18.3.0", - "react": "^18.3.1", - "react-dom": "18.3.1" - }, - "peerDependencies": { - "typescript": "^5.0.0", - "react": "*", - "react-dom": "*" - }, - "dependencies": { - "tsup": "^8.2.4" - } + "name": "usetether", + "version": "1.1.4", + "author": "Braydon (Rainnny) ", + "description": "An API designed to provide real-time access to a user's Discord data.", + "keywords": [ + "java", + "discord", + "api", + "restful", + "realtime" + ], + "homepage": "https://usetether.rest", + "repository": { + "type": "git", + "url": "git+https://github.com/Rainnny7/Tether.git" + }, + "license": "MIT", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsup", + "prepublishOnly": "bun run build" + }, + "files": [ + "dist" + ], + "devDependencies": { + "@types/bun": "latest", + "@types/react": "^18.3.5", + "@types/react-dom": "^18.3.0", + "react": "^18.3.1", + "react-dom": "18.3.1" + }, + "peerDependencies": { + "typescript": "^5.0.0", + "react": "*", + "react-dom": "*" + }, + "dependencies": { + "tsup": "^8.2.4" + } } \ No newline at end of file diff --git a/JS-SDK/src/hook/websocket.ts b/JS-SDK/src/hook/websocket.ts index f03f80a..79c727a 100644 --- a/JS-SDK/src/hook/websocket.ts +++ b/JS-SDK/src/hook/websocket.ts @@ -1,6 +1,7 @@ import { useEffect, useState } from "react"; import { Snowflake } from "@/types/snowflake"; import { TetherConfig } from "@/types/config"; +import { DiscordUser } from "@/types/user"; export const useTetherWS = ( snowflake: Snowflake, @@ -8,12 +9,42 @@ export const useTetherWS = ( endpoint: "usetether.rest", secure: true, } -): Snowflake => { - const [user] = useState(snowflake); +): DiscordUser | undefined => { + const [user] = useState(); + const url: string = `ws${secure && "s"}://${endpoint}/gateway`; useEffect(() => { - console.log("HELLO WORLD", endpoint, secure); - }, [snowflake]); + // Prevent from running on the server + if (typeof window === "undefined") { + return; + } + let socket: WebSocket; // The current WebSocket connection + + /** + * Establish a connection with the API. + */ + function connect() { + console.log("[Tether] Connecting to the WebSocket server..."); + socket = new WebSocket(url); // Connect to the gateway + socket.addEventListener("open", () => { + console.log("[Tether] WebSocket connection established!"); + console.log("attempt to lsn to", snowflake); + }); + // socket.addEventListener("close", connect); + + socket.addEventListener("message", (event) => { + console.log("data:", event.data); + }); + } + + connect(); + + // Cleanup + return () => { + socket.removeEventListener("close", connect); + socket.close(); + }; + }, [url]); return user; };