-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
209 lines (192 loc) · 5.59 KB
/
index.js
File metadata and controls
209 lines (192 loc) · 5.59 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const app = require("express")();
const http = require("http").Server(app);
// const io = require("socket.io")(http);
const io = require("socket.io")(http, {
cors: {
origin: "http://localhost:3000",
},
});
const port = process.env.PORT || 5000;
//
let activeClients = [];
let sockets = [];
function idGenerator(length) {
let result = "";
let characters = "123456789";
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function getClientId() {
const length = 6;
let id = idGenerator(length);
do {
id = idGenerator(length);
} while (
activeClients
.map((activeClient) => {
return activeClient.clientId;
})
.indexOf(id) > -1
);
return id;
}
function addNewClient(socket) {
const clientId = getClientId();
const roomName = "WaitingRoom";
const clientObject = {
socket,
socketId: socket.id,
clientId,
roomName,
};
activeClients.push(clientObject);
return clientId;
}
function isConnectionActive(clientId) {
return activeClients.some((activeClient) => {
return activeClient.clientId === clientId;
});
}
function isConnectionActiveAndWaiting(clientId) {
return activeClients.some((activeClient) => {
return (
activeClient.clientId === clientId &&
activeClient.roomName === "WaitingRoom"
);
});
}
function connectTwoClients(clientSocketId, connectionId) {
let newRoomName = connectionId + new Date().getTime().toString(36);
activeClients.map((activeClient) => {
if (
activeClient.clientId === connectionId ||
activeClient.socketId === clientSocketId
) {
activeClient.roomName = newRoomName;
}
});
return newRoomName;
}
function getRoomNameOfClientBySocketId(socketId) {
let room;
activeClients.map((activeClient) => {
if (activeClient.socketId === socketId) {
room = activeClient.roomName;
}
});
if (!!room && room != "WaitingRoom") {
return room;
} else {
return "Room Not Found!";
}
}
function getSocketsAndResetRoomNameOfConnectedClients(roomName) {
let socketsArray = [];
activeClients.map((activeClient) => {
if (activeClient.roomName === roomName) {
activeClient.roomName = "WaitingRoom";
socketsArray.push(activeClients.socket);
}
});
return socketsArray;
}
function disconnectTwoClients(data) {
activeClients.map((activeClient) => {
if (
(activeClient.clientId === data.clientId &&
activeClient.roomName === data.roomName) ||
(activeClient.clientId === data.connectionId &&
activeClient.roomName === data.roomName)
) {
activeClient.roomName = "WaitingRoom";
}
});
}
function selfConnectCheck(socketId, connectionId) {
return activeClients.some((activeClient) => {
return (
activeClient.socketId === socketId &&
activeClient.clientId === connectionId
);
});
}
io.on("connection", (socket) => {
sockets.push(socket);
// Client Connects
const clientId = addNewClient(socket);
socket.emit("client-id-from-server", clientId);
// Client disconnects
socket.on("disconnect", () => {
const roomName = getRoomNameOfClientBySocketId(socket.id);
socket.to(roomName).emit("goodbye");
const socketsArray = getSocketsAndResetRoomNameOfConnectedClients(roomName);
socketsArray.map((socket) => {
if (!!socket) {
socket.leave(roomName);
}
});
});
// Client sends message
socket.on("send-message", (messageDetails) => {
socket
.to(messageDetails.roomName)
.emit("receive-message", messageDetails.message);
});
socket.on("goodbye", (disconnectionDetails) => {
disconnectTwoClients(disconnectionDetails);
socket.to(disconnectionDetails.roomName).emit("goodbye");
});
// Client tries to connect with someone.
socket.on("join", (connectionId) => {
if (!selfConnectCheck(socket.id, connectionId)) {
if (isConnectionActive(connectionId)) {
if (isConnectionActiveAndWaiting(connectionId)) {
const newRoomName = connectTwoClients(socket.id, connectionId);
if (!!newRoomName) {
//
let activeClientIndex = activeClients.findIndex(
(activeClient) => activeClient.clientId == connectionId
);
let socketObjectOfConnection =
activeClients[activeClientIndex].socket;
// Adding both clients to a new room.
socketObjectOfConnection.join(newRoomName);
socket.join(newRoomName);
//
socket.emit("connection-details", {
status: true,
connectionId,
chatRoom: newRoomName,
});
socketObjectOfConnection.emit("somebody-connected-with-you", {
status: true,
connectionId: clientId,
chatRoom: newRoomName,
});
}
} else {
socket.emit("connection-details", {
status: false,
message: `Ahh, looks like ID ${connectionId} is active but connected with somebody else!`,
});
}
} else {
socket.emit("connection-details", {
status: false,
message: `Oops, found nobody with ID ${connectionId}. Please try later!`,
});
}
} else {
socket.emit("connection-details", {
status: false,
message: `You don't need a chatbox to connect with yourself. Just pay attention to your thoughts! :)`,
});
}
});
});
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`);
});