-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (38 loc) · 1.16 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
const express = require("express");
const app = express();
let firstclient;
let secondclient;
let offerclient;
const port = 4000;
const http = require("http");
const server = http.createServer(app);
const io = require("socket.io")(server);
app.use(express.static(__dirname + "/public"));
let clients = 0;
io.sockets.on("error", e => console.log(e));
io.sockets.on("connection", socket => {
socket.on("ready", () => {
clients++;
console.log("clients",clients)
if (clients > 1){
socket.broadcast.emit("join");
secondclient = socket.id;
} else {
firstclient = socket.id;
}
});
socket.on("offer", (id, message) => {
console.log("offer received")
offerclient = socket.id;
socket.to(secondclient).emit("offer", socket.id, message);
})
socket.on("answer", (id, message) => {
console.log("answer received")
socket.to(offerclient).emit("answer", socket.id, message);
})
socket.on("disconnect", () => {
clients--;
console.log("clients",clients)
});
});
server.listen(port, () => console.log(`Server is running on port ${port}`));