mirror of
https://github.com/arkorty/B.Tech-Project-III.git
synced 2026-04-19 20:51:49 +00:00
init
This commit is contained in:
37
negot8/dashboard/lib/socket.ts
Normal file
37
negot8/dashboard/lib/socket.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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 });
|
||||
}
|
||||
Reference in New Issue
Block a user