-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket.ts
More file actions
77 lines (63 loc) · 2.06 KB
/
socket.ts
File metadata and controls
77 lines (63 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//TODO: (1) change cors in backend
//TODO: (2) set withCredentials: true
import { io } from "socket.io-client";
import { getSessionUser } from "./utils/getSessionUser";
// Create socket but don't connect automatically
export const socket = io(process.env.NEXT_PUBLIC_BACKEND_URI, {
transports: ['polling', 'websocket'],
autoConnect: false, // Changed to false to prevent auto-connection
reconnection: true,
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
timeout: 20000,
forceNew: true
});
// Initialize socket connection with authentication
export async function initializeSocket() {
try {
const user = await getSessionUser();
if (!user) {
console.warn("No session user found. Socket connection aborted.");
return { success: false, message: "No user session" };
}
// Set auth data before connecting
socket.auth = { user };
// Connect manually now that we have auth data
socket.connect();
return { success: true };
} catch (error) {
console.error("Failed to initialize socket:", error);
if (error instanceof Error) {
return { success: false, message: error.message };
} else {
return { success: false, message: "Internal server error" };
}
}
}
// Set up event listeners
socket.on("connect", () => {
console.log("Socket connected");
});
socket.on("connected", () => {
console.log("Server confirmed connection for user");
});
socket.on("connect_error", (error) => {
console.warn("Socket connection error:", error);
});
socket.on("error", (error) => {
console.warn("Socket error:", error);
});
socket.on("disconnect", (reason) => {
console.log("Socket disconnected:", reason);
// If the server disconnected us, don't try to reconnect automatically
if (reason === "io server disconnect") {
console.log("Server disconnected the socket, reconnection must be manual");
}
});
// Export a reconnect function for manual reconnection if needed
export function reconnectSocket() {
if (!socket.connected) {
initializeSocket();
}
}