This commit is contained in:
payacom
2026-07-28 10:06:42 +03:30
parent a1d7db5e7e
commit 67f59eefdf
129 changed files with 7964 additions and 1561 deletions

View File

@@ -1,27 +1,33 @@
import { io, Socket } from "socket.io-client";
import Cookies from "js-cookie";
import { SOCKET_URL } from "@/components/main/BaseUrl";
let sharedSocket: Socket | null = null;
let refCount = 0;
const connectionListeners = new Set<(connected: boolean) => void>();
type PendingGroupJoin = { roomId: string; userId: string };
type PendingGroupJoin = { roomId: string };
const pendingGroupJoins = new Map<string, PendingGroupJoin>();
let pendingUserId: string | null = null;
let pendingUserJoin = false;
function notifyConnectionListeners(connected: boolean) {
connectionListeners.forEach((fn) => fn(connected));
}
function getAuthToken(): string {
if (typeof window === "undefined") return "";
return Cookies.get("token") || "";
}
function flushPendingJoins(): void {
if (!sharedSocket?.connected) return;
if (pendingUserId) {
sharedSocket.emit("joinUser", { userId: pendingUserId });
if (pendingUserJoin) {
sharedSocket.emit("joinUser");
}
for (const { roomId, userId } of pendingGroupJoins.values()) {
sharedSocket.emit("joinGroupRoom", { roomId, userId });
for (const { roomId } of pendingGroupJoins.values()) {
sharedSocket.emit("joinGroupRoom", { roomId });
}
}
@@ -41,6 +47,9 @@ export function acquireChatSocket(): Socket {
timeout: 20000,
withCredentials: true,
autoConnect: true,
auth: (cb) => {
cb({ token: getAuthToken() });
},
});
sharedSocket.on("connect", () => {
@@ -69,34 +78,30 @@ export function disconnectChatSocket(): void {
sharedSocket = null;
refCount = 0;
pendingGroupJoins.clear();
pendingUserId = null;
pendingUserJoin = false;
notifyConnectionListeners(false);
}
}
export function joinUserRoom(userId: string): void {
const id = String(userId).trim();
if (!id) return;
pendingUserId = id;
export function joinUserRoom(_userId?: string): void {
pendingUserJoin = true;
if (sharedSocket?.connected) {
sharedSocket.emit("joinUser", { userId: id });
sharedSocket.emit("joinUser");
}
}
export function joinChatRoom(userId: string, receiverId: string): void {
const uid = String(userId).trim();
export function joinChatRoom(_userId: string, receiverId: string): void {
const rid = String(receiverId).trim();
if (!uid || !rid || !sharedSocket) return;
sharedSocket.emit("joinChat", { userId: uid, receiverId: rid });
if (!rid || !sharedSocket) return;
sharedSocket.emit("joinChat", { receiverId: rid });
}
export function joinGroupRoom(roomId: string, userId: string): void {
export function joinGroupRoom(roomId: string, _userId?: string): void {
const rid = String(roomId).trim();
const uid = String(userId).trim();
if (!rid || !uid) return;
pendingGroupJoins.set(rid, { roomId: rid, userId: uid });
if (!rid) return;
pendingGroupJoins.set(rid, { roomId: rid });
if (sharedSocket?.connected) {
sharedSocket.emit("joinGroupRoom", { roomId: rid, userId: uid });
sharedSocket.emit("joinGroupRoom", { roomId: rid });
}
}