-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathserver.js
57 lines (48 loc) · 1.5 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 app = express();
const bodyParser = require('body-parser');
const webrtc = require("wrtc");
let senderStream;
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/consumer", async ({ body }, res) => {
const peer = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.stunprotocol.org"
}
]
});
const desc = new webrtc.RTCSessionDescription(body.sdp);
await peer.setRemoteDescription(desc);
senderStream.getTracks().forEach(track => peer.addTrack(track, senderStream));
const answer = await peer.createAnswer();
await peer.setLocalDescription(answer);
const payload = {
sdp: peer.localDescription
}
res.json(payload);
});
app.post('/broadcast', async ({ body }, res) => {
const peer = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.stunprotocol.org"
}
]
});
peer.ontrack = (e) => handleTrackEvent(e, peer);
const desc = new webrtc.RTCSessionDescription(body.sdp);
await peer.setRemoteDescription(desc);
const answer = await peer.createAnswer();
await peer.setLocalDescription(answer);
const payload = {
sdp: peer.localDescription
}
res.json(payload);
});
function handleTrackEvent(e, peer) {
senderStream = e.streams[0];
};
app.listen(5000, () => console.log('server started'));