This commit is contained in:
parent
0a17c788a1
commit
1f0b990124
BIN
JS-SDK/bun.lockb
BIN
JS-SDK/bun.lockb
Binary file not shown.
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "usetether",
|
"name": "usetether",
|
||||||
"version": "1.1.3",
|
"version": "1.1.4",
|
||||||
"author": "Braydon (Rainnny) <braydonrainnny@gmail.com>",
|
"author": "Braydon (Rainnny) <braydonrainnny@gmail.com>",
|
||||||
"description": "An API designed to provide real-time access to a user's Discord data.",
|
"description": "An API designed to provide real-time access to a user's Discord data.",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Snowflake } from "@/types/snowflake";
|
import { Snowflake } from "@/types/snowflake";
|
||||||
import { TetherConfig } from "@/types/config";
|
import { TetherConfig } from "@/types/config";
|
||||||
|
import { DiscordUser } from "@/types/user";
|
||||||
|
|
||||||
export const useTetherWS = (
|
export const useTetherWS = (
|
||||||
snowflake: Snowflake,
|
snowflake: Snowflake,
|
||||||
@ -8,12 +9,42 @@ export const useTetherWS = (
|
|||||||
endpoint: "usetether.rest",
|
endpoint: "usetether.rest",
|
||||||
secure: true,
|
secure: true,
|
||||||
}
|
}
|
||||||
): Snowflake => {
|
): DiscordUser | undefined => {
|
||||||
const [user] = useState<Snowflake>(snowflake);
|
const [user] = useState<DiscordUser | undefined>();
|
||||||
|
|
||||||
|
const url: string = `ws${secure && "s"}://${endpoint}/gateway`;
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("HELLO WORLD", endpoint, secure);
|
// Prevent from running on the server
|
||||||
}, [snowflake]);
|
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;
|
return user;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user