Ramble-FE / lib / stompClient.ts
stompClient.ts
Raw
import { tokenStorage } from "@/utils";
import { Client } from "@stomp/stompjs";
import Constants from "expo-constants";
import SockJS from "sockjs-client";

const API_BASE_URL = Constants.expoConfig?.extra?.apiBaseUrl as string;

export async function stompClient() {
    const client = new Client({
        webSocketFactory: () => new SockJS(`${API_BASE_URL}/ws`),
        reconnectDelay: 5000, // 재연결 시도 시간
        heartbeatIncoming: 4000,
        heartbeatOutgoing: 4000,
        // debug: (str) => console.log("[STOMP]", str),
        beforeConnect: async (c) => {
            const token = await tokenStorage.getAccessToken();
            c.connectHeaders = token ? { Authorization: `Bearer ${token}` } : {};
        },
    });

    return client;
}