-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
84 lines (70 loc) · 2.77 KB
/
server.ts
File metadata and controls
84 lines (70 loc) · 2.77 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
78
79
80
81
82
83
84
import { createServer } from "node:http";
import { Server } from "socket.io";
type UserInfo = { id: string; name: string; email: string; image: string };
type SocketUserInfo = { userId: string; fileId: string };
declare global {
var usersDetails: Record<string, Map<string, UserInfo>> | undefined;
}
declare global{
var socketToUser: Map<string,SocketUserInfo> | undefined;
}
export const user = globalThis.usersDetails || (globalThis.usersDetails = {});
export const socketToUser = globalThis.socketToUser || (globalThis.socketToUser= new Map());
const port = 8000;
const httpserver = createServer();
const io = new Server(httpserver, { cors: { origin: "*" } });
io.on("connection", (socket) => {
socket.on("createRoom", ({ fileId, joinedUser }) => {
socket.join(fileId);
if (!user[fileId]) {
user[fileId] = new Map();
}
if (joinedUser) {
user[fileId].set(joinedUser.id, joinedUser);
socketToUser.set(socket.id, { userId: joinedUser.id, fileId });
console.log("joined after the connection .....................", joinedUser)
console.log("users available in the map of user with the file and details of user .......................",user[fileId]);
console.log("socket with user is attached ",socketToUser);
}
io.to(fileId).emit("user-Joined", Array.from(user[fileId].values())); //send it to every user
});
socket.on("send-changes", ({ delta, fileId }) => {
console.log("CHANGE");
console.log(fileId,"fileid is here ");
console.log(delta,"detla")
socket.to(fileId).emit("receive-changes", { delta, fileId });
});
socket.on("cursor-move", (range, cursorid, fileid) => {
console.log("fired for cursor")
socket.to(fileid).emit("receive-cursor-move", range, fileid, cursorid);
});
socket.on("disconnect", () => {
const userInfo = socketToUser.get(socket.id);
console.log("socketid : ..................",socket.id)
if (userInfo) {
const { userId, fileId } = userInfo;
if(user[fileId]){
console.log("got disconnected...................",user[fileId].get(userId));
user[fileId].delete(userId);
io.to(fileId).emit("user-Joined",Array.from(user[fileId].values()));
console.log("user is in the file after deletion.................",user[fileId]);
}
}
socketToUser.delete(socket.id);
console.log("current socket.............with the user ",socketToUser)
});
});
httpserver.listen(port, () =>
console.log(`Socket.IO server running on ${port}`)
);
// Handle server errors
httpserver.on("error", (error) => {
console.error("🔴 HTTP Server error:", error);
});
process.on("SIGINT", () => {
console.log("\n🛑 Shutting down server...");
httpserver.close(() => {
console.log("✅ Server closed");
process.exit(0);
});
});