-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (120 loc) · 3.07 KB
/
Copy pathserver.js
File metadata and controls
142 lines (120 loc) · 3.07 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Installing Dependencies
const http = require("http");
const app = require("./app");
const { Server } = require("socket.io");
// Assigning a port
const normalizePort = (val) => {
const port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
};
const port = normalizePort(process.env.PORT || "3123");
app.set("port", port);
const errorHandler = (error) => {
if (error.syscall !== "listen") {
throw error;
}
const address = server.address();
const bind =
typeof address === "string" ? "pipe " + address : "port: " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges.");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use.");
process.exit(1);
break;
default:
throw error;
}
};
// Creating a server
const server = http.createServer(app);
// Error Handling
server.on("error", errorHandler);
server.on("listening", () => {
const address = server.address();
const bind = typeof address === "string" ? "pipe " + address : "port " + port;
console.log("Listening on " + bind);
});
// Settup up a Web Socket
const urlForProd =
process.env.NODE_ENV == "production"
? "https://groupomernia.vercel.app"
: "http://localhost:5173";
const io = new Server(server, {
cors: {
origin: `${urlForProd}`,
methods: ["GET", "POST"],
credentials: true,
},
});
const onlineUsers = new Set();
const userSockets = {};
io.on("connection", (socket) => {
// Add user to online users set
socket.on("addUser", (userId) => {
userSockets[userId] = socket.id;
onlineUsers.add(userId);
io.emit("onlineUsers", Array.from(onlineUsers));
});
// Handling a New Post
socket.on("post", () => {
io.emit("newPost");
});
// Handle a Friend Request
socket.on(
"friendRequest",
({ senderId, recipientId, senderName, senderPfp }) => {
const recipientSocketId = userSockets[recipientId];
// console.log(senderName, recipientSocketId);
if (recipientSocketId) {
io.to(recipientSocketId).emit("receivedFriendRequest", {
senderPfp,
senderId,
senderName,
date: new Date().toLocaleTimeString(),
});
}
}
);
// Handling a Denied Friend Request
socket.on("friendRequestDeny", ({ senderId, recipientId }) => {
const recipientSocketId = userSockets[recipientId];
if (recipientSocketId) {
io.to(recipientSocketId).emit("deniedFriendRequest", {
senderId,
senderName,
});
}
});
// Handling an Accepted Friend Request
socket.on(
"friendRequestAccept",
({ senderId, recipientId, senderName, senderPfp }) => {
const recipientSocketId = userSockets[recipientId];
if (recipientSocketId) {
io.to(recipientSocketId).emit("acceptedFriendRequest", {
senderPfp,
senderId,
senderName,
date: new Date().toLocaleTimeString(),
});
}
}
);
// When a user disconnects, remove them from the online set
socket.on("disconnect", () => {
onlineUsers.delete(socket.userId);
io.emit("onlineUsers", Array.from(onlineUsers));
});
});
// Active Server Listening
server.listen(port);