-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (113 loc) · 4 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
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
const express = require("express");
const app = express();
const webrtc = require("wrtc");
fs = require("fs");
require("dotenv").config();
var port = process.env.PORT || 3000;
let senderStream = [];
let peerUser = [];
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(function (req, res, next) {
const allowedOrigins = [
"http://127.0.0.1:3000",
"http://localhost:3000",
"http://localhost:8000",
"http://127.0.0.1:8080",
"http://localhost:9000",
"https://webrct-sfu-demo.herokuapp.com/",
];
const origin = req.headers.origin;
// Website you wish to allow to connect
if (allowedOrigins.includes(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
// Request methods you wish to allow
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
// Request headers you wish to allow
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept,x-socket-id,x-csrf-token"
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader("Access-Control-Allow-Credentials", true);
// Pass to next layer of middleware
next();
});
app.post("/consumer/:roomID/:userID", async (req, res) => {
if (req.params.roomID === "null")
return res.status(401).json({ error: "no room id" });
if (req.params.userID === "null")
return res.status(401).json({ error: "no user id" });
var roomID = parseInt(req.params.roomID);
var userID = parseInt(req.params.userID);
if (typeof senderStream[roomID] === "undefined")
return res.status(401).json({ error: "sender not found" });
if (peerUser[userID]) await peerUser[userID].close();
peerUser[userID] = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: process.env.STUN_URL,
},
],
});
const desc = new webrtc.RTCSessionDescription(req.body.sdp);
await peerUser[userID].setRemoteDescription(desc);
senderStream[roomID]
.getTracks()
.forEach((track) => peerUser[userID].addTrack(track, senderStream[roomID]));
const answer = await peerUser[userID].createAnswer();
await peerUser[userID].setLocalDescription(answer);
const payload = {
sdp: peerUser[userID].localDescription,
};
res.json(payload);
});
app.post("/disconnect/:roomID/:userID", async (req, res) => {
if (req.params.roomID === "null")
return res.status(401).json({ error: "no room id" });
if (req.params.userID === "null")
return res.status(401).json({ error: "no user id" });
var roomID = parseInt(req.params.roomID);
var userID = parseInt(req.params.userID);
if (typeof peerUser[userID] === "undefined")
return res.status(401).json({ error: "peer not found" });
peerUser[userID].close();
delete peerUser[userID];
return res.json({ success: "clear peer succees" });
});
app.post("/broadcast/:roomID/:userID", async (req, res) => {
if (req.params.roomID === "null")
return res.status(401).json({ error: "no room id" });
if (req.params.userID === "null")
return res.status(401).json({ error: "no user id" });
if (peerUser[userID]) return res.json({ error: "user already exist" });
var roomID = parseInt(req.params.roomID);
var userID = parseInt(req.params.userID);
peerUser[userID] = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: process.env.STUN_URL,
},
],
});
peerUser[userID].ontrack = (e) =>
handleTrackEvent(e, peerUser[userID], roomID);
const desc = new webrtc.RTCSessionDescription(req.body.sdp);
await peerUser[userID].setRemoteDescription(desc);
const answer = await peerUser[userID].createAnswer();
await peerUser[userID].setLocalDescription(answer);
const payload = {
sdp: peerUser[userID].localDescription,
};
return res.json(payload);
});
function handleTrackEvent(e, peer, roomID) {
senderStream[roomID] = e.streams[0];
}
app.listen(port, () => console.log("server started : " + port));