mirror of
https://github.com/arkorty/B.Tech-Project-III.git
synced 2026-04-19 12:41:48 +00:00
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
// Socket.IO client singleton — import this anywhere to get the shared socket
|
|
|
|
import { io, Socket } from "socket.io-client";
|
|
|
|
const API_URL = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000";
|
|
|
|
let socket: Socket | null = null;
|
|
|
|
export function getSocket(): Socket {
|
|
if (!socket) {
|
|
socket = io(API_URL, {
|
|
transports: ["websocket", "polling"],
|
|
autoConnect: true,
|
|
reconnectionAttempts: 5,
|
|
reconnectionDelay: 1500,
|
|
});
|
|
|
|
socket.on("connect", () => {
|
|
console.log("[Socket.IO] connected:", socket?.id);
|
|
});
|
|
socket.on("disconnect", (reason) => {
|
|
console.log("[Socket.IO] disconnected:", reason);
|
|
});
|
|
socket.on("connect_error", (err) => {
|
|
console.warn("[Socket.IO] connection error:", err.message);
|
|
});
|
|
}
|
|
return socket;
|
|
}
|
|
|
|
export function joinNegotiation(negotiationId: string) {
|
|
getSocket().emit("join_negotiation", { negotiation_id: negotiationId });
|
|
}
|
|
|
|
export function leaveNegotiation(negotiationId: string) {
|
|
getSocket().emit("leave_negotiation", { negotiation_id: negotiationId });
|
|
}
|