Skip to content

Commit

Permalink
add ping event
Browse files Browse the repository at this point in the history
  • Loading branch information
SuspiciousLookingOwl committed Jun 19, 2024
1 parent 78c2220 commit 6a634ac
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/libs/queue/providers/queue/hooks/queue-events.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const useQueueEvents = () => {
const api = useApi();
let ws: WebSocket | undefined;
let reconnectTimeout: NodeJS.Timeout;
let pingInterval: NodeJS.Timeout;
let isAuthenticated = false;
let isConnected = false;
const emitter = new EventEmitter() as TypedEmitter<QueueEvents>;
Expand All @@ -86,9 +87,15 @@ export const useQueueEvents = () => {
};
ws.onopen = async () => {
isConnected = true;

clearInterval(pingInterval);
pingInterval = setInterval(() => send("ping"), 30000);

send("identify", { token: api.authManager.getAccessToken() });
};
ws.onclose = (ev) => {
clearInterval(pingInterval);

if (ev.code === 3333) return;

if (isAuthenticated) {
Expand Down Expand Up @@ -122,7 +129,7 @@ export const useQueueEvents = () => {
clearTimeout(reconnectTimeout);
});

const send = (event: string, data: unknown) => {
const send = (event: string, data?: unknown) => {
const message = JSON.stringify({ event, data });
ws?.send(message);
};
Expand Down

0 comments on commit 6a634ac

Please sign in to comment.