-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
144 lines (100 loc) · 3.63 KB
/
index.js
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
const express = require("express");
const socketio = require("socket.io");
const app = express();
app.use(express.static(__dirname + "/public"));
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () =>
console.log(`Server has started on port ${PORT}`)
);
const io = socketio(server);
let sockets = [];
let searching = [];
let notAvailable = [];
io.on("connection", async (socket) => {
sockets.push(socket);
const allSockets = await io.allSockets();
io.emit("numberOfOnline", allSockets.size);
socket.on("start", (id) => {
sockets = sockets.filter((s) => {
if (s.id === id) {
searching.push(s);
return;
} else {
return s;
}
});
let i = 0;
while (i < searching.length) {
const peer = searching[i];
if (peer.id !== id) {
searching = searching.filter((s) => s.id !== peer.id);
searching = searching.filter((s) => s.id !== id);
notAvailable.push(socket, peer);
const socketRoomToLeave = [...socket.rooms][1];
const peerRoomToLeave = [...peer.rooms][1];
socket.leave(socketRoomToLeave);
peer.leave(peerRoomToLeave);
const roomName = `${id}#${peer.id}`;
socket.join(roomName);
peer.join(roomName);
io.of("/")
.to(roomName)
.emit("chatStart", "You are now chatting with a random stranger");
break;
}
socket.emit("searching", "Searching...");
i++;
}
});
socket.on("newMessageToServer", (msg) => {
// get room
const roomName = [...socket.rooms][1];
io.of("/").to(roomName).emit("newMessageToClient", { id: socket.id, msg });
});
socket.on("typing", (msg) => {
const roomName = [...socket.rooms][1];
const ids = roomName.split("#");
const peerId = ids[0] === socket.id ? ids[1] : ids[0];
const peer = notAvailable.find((user) => user.id === peerId);
peer.emit("strangerIsTyping", msg);
});
socket.on("doneTyping", () => {
const roomName = [...socket.rooms][1];
const ids = roomName.split("#");
const peerId = ids[0] === socket.id ? ids[1] : ids[0];
const peer = notAvailable.find((user) => user.id === peerId);
peer.emit("strangerIsDoneTyping");
});
socket.on("stop", () => {
const roomName = [...socket.rooms][1];
const ids = roomName.split("#");
const peerId = ids[0] === socket.id ? ids[1] : ids[0];
const peer = notAvailable.find((user) => user.id === peerId);
peer.leave(roomName);
socket.leave(roomName);
peer.emit("strangerDisconnected", "Stranger has disconnected");
socket.emit("endChat", "You have disconnected");
notAvailable = notAvailable.filter((user) => user.id !== socket.id);
notAvailable = notAvailable.filter((user) => user.id !== peer.id);
sockets.push(socket, peer);
});
socket.on("disconnecting", async () => {
const roomName = [...socket.rooms][1];
if (roomName) {
io.of("/").to(roomName).emit("goodBye", "Stranger has disconnected");
const ids = roomName.split("#");
const peerId = ids[0] === socket.id ? ids[1] : ids[0];
const peer = notAvailable.find((user) => user.id === peerId);
peer.leave(roomName);
notAvailable = notAvailable.filter((user) => user.id !== peerId);
sockets.push(peer);
}
sockets = sockets.filter((user) => user.id !== socket.id);
searching = searching.filter((user) => user.id !== socket.id);
notAvailable = notAvailable.filter((user) => user.id !== socket.id);
});
socket.on("disconnect", async () => {
const allSockets = await io.allSockets();
io.emit("numberOfOnline", allSockets.size);
});
});