-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
57 lines (47 loc) · 1.23 KB
/
server.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
const express = require("express")
const http = require("http")
const app = express()
const server = http.createServer(app)
const io = require("socket.io")(server, {
cors: {
origin : "http://localhost:3000",
methods: [ "GET", "POST" ]
}
})
let users = [];
io.on("connection", (socket) => {
if (!users.find(user => user === socket.id)) {
users[users.length] = socket.id
}
socket.emit("me", socket.id)
io.emit("allUsers", users)
socket.on("call-user", (data) => {
if(data.from){
io.to(data.to).emit("call", { from: data.from, signalData: data.signalData })
}
})
socket.on("answer-call", (data) => {
if(data.from){
io.to(data.to).emit("call-answer", { signalData: data.signalData, from: data.from })
}
})
socket.on("refuse-call", (data) => {
if(data.to){
io.to(data.to).emit("call-refused", { from: data.from })
}
})
socket.on("join-room", (data) => {
if(data.to){
io.to(data.to).emit("join-room", { roomId: data.roomId })
}
})
socket.on('hang-up', (data) => {
if(data.from){
io.emit("hanging-up", { from: data.from })
}
})
socket.on('disconnect', () => {
users = users.filter(user => user !== socket.id)
})
})
server.listen(8000, () => console.log("server is running on port 5000"))