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",
|
"name": "use-tether",
|
||||||
"version": "1.0.1",
|
"version": "1.0.2",
|
||||||
"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": [
|
||||||
|
@ -19,7 +19,7 @@ export const useTetherWS = (
|
|||||||
secure: true,
|
secure: true,
|
||||||
}
|
}
|
||||||
): DiscordUser | undefined => {
|
): DiscordUser | undefined => {
|
||||||
const { endpoint, secure } = config;
|
const { endpoint, secure, debug } = config;
|
||||||
const url: string = `ws${secure && "s"}://${endpoint}/gateway`;
|
const url: string = `ws${secure && "s"}://${endpoint}/gateway`;
|
||||||
const [user, setUser] = useState<DiscordUser | undefined>();
|
const [user, setUser] = useState<DiscordUser | undefined>();
|
||||||
|
|
||||||
@ -35,19 +35,34 @@ export const useTetherWS = (
|
|||||||
*/
|
*/
|
||||||
const connect = () => {
|
const connect = () => {
|
||||||
console.log("[Tether] Connecting to the WebSocket server...");
|
console.log("[Tether] Connecting to the WebSocket server...");
|
||||||
|
if (debug) {
|
||||||
|
console.debug("[Tether] Endpoint:", url);
|
||||||
|
}
|
||||||
socket = new WebSocket(url); // Connect to the gateway
|
socket = new WebSocket(url); // Connect to the gateway
|
||||||
|
|
||||||
// Track the user when the WS connects
|
// Track the user when the WS connects
|
||||||
socket.addEventListener("open", () => {
|
socket.addEventListener("open", () => {
|
||||||
|
if (debug) {
|
||||||
|
console.debug("[Tether] Sending listen to user packet...");
|
||||||
|
}
|
||||||
socket.send(JSON.stringify({ op: 0, snowflake: snowflake }));
|
socket.send(JSON.stringify({ op: 0, snowflake: snowflake }));
|
||||||
console.log("[Tether] WebSocket connection established!");
|
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) => {
|
socket.addEventListener("message", (event) => {
|
||||||
const statusPacket: UserStatusPacket = JSON.parse(
|
const json = JSON.parse(event.data);
|
||||||
event.data
|
if (debug) {
|
||||||
) as UserStatusPacket;
|
console.debug("[Tether] Received Packet:", json);
|
||||||
|
}
|
||||||
|
const statusPacket: UserStatusPacket = json as UserStatusPacket;
|
||||||
if (statusPacket.op === 1) {
|
if (statusPacket.op === 1) {
|
||||||
setUser(statusPacket.user);
|
setUser(statusPacket.user);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ export * from "@/hook/websocket";
|
|||||||
export * from "@/types/config";
|
export * from "@/types/config";
|
||||||
export * from "@/types/snowflake";
|
export * from "@/types/snowflake";
|
||||||
|
|
||||||
export * from "@/types/user/discord-user";
|
|
||||||
export * from "@/types/user/user-flags";
|
export * from "@/types/user/user-flags";
|
||||||
export * from "@/types/user/avatar/avatar";
|
export * from "@/types/user/avatar/avatar";
|
||||||
export * from "@/types/user/avatar/decoration-asset";
|
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-badge";
|
||||||
export * from "@/types/user/clan/clan";
|
export * from "@/types/user/clan/clan";
|
||||||
export * from "@/types/user/nitro-subscription";
|
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.
|
* Whether the connection should be secure.
|
||||||
*/
|
*/
|
||||||
secure?: boolean;
|
secure?: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to enable debugging.
|
||||||
|
*/
|
||||||
|
debug?: boolean;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user