Add debug option and potentially fix types?
All checks were successful
Publish JS SDK / docker (push) Successful in 13s
All checks were successful
Publish JS SDK / docker (push) Successful in 13s
This commit is contained in:
parent
8ac403d185
commit
cd4a57a4f2
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "use-tether",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"author": "Braydon (Rainnny) <braydonrainnny@gmail.com>",
|
||||
"description": "An API designed to provide real-time access to a user's Discord data.",
|
||||
"keywords": [
|
||||
|
@ -19,7 +19,7 @@ export const useTetherWS = (
|
||||
secure: true,
|
||||
}
|
||||
): DiscordUser | undefined => {
|
||||
const { endpoint, secure } = config;
|
||||
const { endpoint, secure, debug } = config;
|
||||
const url: string = `ws${secure && "s"}://${endpoint}/gateway`;
|
||||
const [user, setUser] = useState<DiscordUser | undefined>();
|
||||
|
||||
@ -35,19 +35,34 @@ export const useTetherWS = (
|
||||
*/
|
||||
const connect = () => {
|
||||
console.log("[Tether] Connecting to the WebSocket server...");
|
||||
if (debug) {
|
||||
console.debug("[Tether] Endpoint:", url);
|
||||
}
|
||||
socket = new WebSocket(url); // Connect to the gateway
|
||||
|
||||
// Track the user when the WS connects
|
||||
socket.addEventListener("open", () => {
|
||||
if (debug) {
|
||||
console.debug("[Tether] Sending listen to user packet...");
|
||||
}
|
||||
socket.send(JSON.stringify({ op: 0, snowflake: snowflake }));
|
||||
console.log("[Tether] WebSocket connection established!");
|
||||
});
|
||||
socket.addEventListener("close", connect); // Reconnect on close
|
||||
socket.addEventListener("close", () => {
|
||||
if (debug) {
|
||||
console.debug(
|
||||
"[Tether] Connection to the WS server was lost, reconnecting..."
|
||||
);
|
||||
}
|
||||
connect();
|
||||
}); // Reconnect on close
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
const statusPacket: UserStatusPacket = JSON.parse(
|
||||
event.data
|
||||
) as UserStatusPacket;
|
||||
const json = JSON.parse(event.data);
|
||||
if (debug) {
|
||||
console.debug("[Tether] Received Packet:", json);
|
||||
}
|
||||
const statusPacket: UserStatusPacket = json as UserStatusPacket;
|
||||
if (statusPacket.op === 1) {
|
||||
setUser(statusPacket.user);
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ export * from "@/hook/websocket";
|
||||
export * from "@/types/config";
|
||||
export * from "@/types/snowflake";
|
||||
|
||||
export * from "@/types/user/discord-user";
|
||||
export * from "@/types/user/user-flags";
|
||||
export * from "@/types/user/avatar/avatar";
|
||||
export * from "@/types/user/avatar/decoration-asset";
|
||||
@ -19,3 +18,4 @@ export * from "@/types/user/connected-account";
|
||||
export * from "@/types/user/clan/clan-badge";
|
||||
export * from "@/types/user/clan/clan";
|
||||
export * from "@/types/user/nitro-subscription";
|
||||
export * from "@/types/user/discord-user";
|
||||
|
@ -8,4 +8,9 @@ export type TetherConfig = {
|
||||
* Whether the connection should be secure.
|
||||
*/
|
||||
secure?: boolean;
|
||||
|
||||
/**
|
||||
* Whether to enable debugging.
|
||||
*/
|
||||
debug?: boolean;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user