-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·64 lines (52 loc) · 1.44 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
const express = require("express");
const app = express();
const http = require("http").createServer(app)
const io = require('socket.io')(http);
const {join} = require("path");
let activeSockets = [];
app.use(express.static(join(__dirname,'public')));
http.listen(3000, () => {
console.log("app started on 3000");
});
io.on("connection", socket => {
const existingSocket = activeSockets.find(
existingSocket => existingSocket === socket.id
);
if (!existingSocket) {
activeSockets.push(socket.id);
socket.emit("update-user-list", {
users: activeSockets.filter(
existingSocket => existingSocket !== socket.id
)
});
socket.broadcast.emit("update-user-list", {
users: [socket.id]
});
}
socket.on('disconnect',() => {
console.log("bye bye 👋");
activeSockets = activeSockets.filter(existingSocket => existingSocket !== socket.id);
socket.broadcast.emit("remove-user",{
socketId: socket.id
});
})
socket.on("call-user", data => {
socket.to(data.to).emit("call-made", {
offer: data.offer,
socket: socket.id
});
});
socket.on("make-answer", data => {
socket.to(data.to).emit("answer-made", {
socket: socket.id,
answer: data.answer
});
});
socket.on("reject-call", data => {
socket.to(data.from).emit("call-rejected", {
socket: socket.id
});
});
console.log("connection estblished by: "+socket.id);
console.log(activeSockets);
})