-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (40 loc) · 1.08 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
const express = require("express");
const socketio = require("socket.io");
const peer = require("simple-peer");
const http = require("http");
const cors = require("cors");
const app = express();
const server = http.createServer(app);
const path = require("path");
const io = socketio(server);
app.use(cors());
app.use(express.static(path.join(__dirname, "/public")));
const port = process.env.PORT || 3000;
//
let clients = 0;
io.on("connection", socket => {
socket.on("permission_success", () => {
if (clients < 2) {
if (clients == 1) {
console.log(clients);
socket.emit("makePeer");
}
} else {
socket.emit("sessionActive");
console.log("err");
}
clients++;
});
socket.on("offer", offer => {
socket.broadcast.emit("backReply", offer);
});
socket.on("Answer", data => {
socket.broadcast.emit("backanswer", data);
});
socket.on("disconnect", () => {
if (clients > 0) {
clients -= 1;
}
});
});
server.listen(port);