-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
138 lines (109 loc) · 3.49 KB
/
app.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
const express = require("express");
const http = require("http");
const PORT = process.env.PORT || 3000;
const app = express();
const server = http.createServer(app);
const io = require("socket.io")(server);
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
let connectedPeers = [];
let connectedPeersStrangers = [];
io.on("connection", (socket) => {
connectedPeers.push(socket.id);
socket.on("pre-offer", (data) => {
console.log("pre-offer-came");
const { calleePersonalCode, callType } = data;
console.log(calleePersonalCode);
console.log(connectedPeers);
const connectedPeer = connectedPeers.find(
(peerSocketId) => peerSocketId === calleePersonalCode
);
console.log(connectedPeer);
if (connectedPeer) {
const data = {
callerSocketId: socket.id,
callType,
};
io.to(calleePersonalCode).emit("pre-offer", data);
} else {
const data = {
preOfferAnswer: "CALLEE_NOT_FOUND",
};
io.to(socket.id).emit("pre-offer-answer", data);
}
});
socket.on("pre-offer-answer", (data) => {
const { callerSocketId } = data;
const connectedPeer = connectedPeers.find(
(peerSocketId) => peerSocketId === callerSocketId
);
if (connectedPeer) {
io.to(data.callerSocketId).emit("pre-offer-answer", data);
}
});
socket.on("webRTC-signaling", (data) => {
const { connectedUserSocketId } = data;
const connectedPeer = connectedPeers.find(
(peerSocketId) => peerSocketId === connectedUserSocketId
);
if (connectedPeer) {
io.to(connectedUserSocketId).emit("webRTC-signaling", data);
}
});
socket.on("user-hanged-up", (data) => {
const { connectedUserSocketId } = data;
const connectedPeer = connectedPeers.find(
(peerSocketId) => peerSocketId === connectedUserSocketId
);
if (connectedPeer) {
io.to(connectedUserSocketId).emit("user-hanged-up");
}
});
socket.on("stranger-connection-status", (data) => {
const { status } = data;
if (status) {
connectedPeersStrangers.push(socket.id);
} else {
const newConnectedPeersStrangers = connectedPeersStrangers.filter(
(peerSocketId) => peerSocketId !== socket.id
);
connectedPeersStrangers = newConnectedPeersStrangers;
}
console.log(connectedPeersStrangers)
});
socket.on("get-stranger-socket-id", () => {
let randomStrangerSocketId;
const filterConnectedPeersStrangers = connectedPeersStrangers.filter(
(peerSocketId) => peerSocketId !== socket.id
);
if (filterConnectedPeersStrangers.length > 0) {
randomStrangerSocketId =
filterConnectedPeersStrangers[
Math.floor(Math.random() * filterConnectedPeersStrangers.length)
];
} else {
randomStrangerSocketId = null;
}
const data = {
randomStrangerSocketId
};
io.to(socket.id).emit("stranger-socket-id", data);
});
socket.on("disconnect", () => {
console.log("user disconnected");
const newConnectedPeers = connectedPeers.filter(
(peerSocketId) => peerSocketId !== socket.id
);
connectedPeers = newConnectedPeers;
//console.log(connectedPeers);
const newConnectedPeersStrangers = connectedPeersStrangers.filter(
(peerSocketId) => peerSocketId !== socket.id
);
connectedPeersStrangers = newConnectedPeersStrangers;
});
});
server.listen(PORT, () => {
console.log(`listening on ${PORT}`);
});