Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,54 +1,57 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const app = express();
|
| 3 |
const bodyParser = require('body-parser');
|
| 4 |
-
const webrtc = require(
|
| 5 |
|
| 6 |
-
|
| 7 |
-
const roomStreams = {}; // { [roomId]: MediaStream }
|
| 8 |
|
| 9 |
app.use(express.static('public'));
|
| 10 |
app.use(bodyParser.json());
|
| 11 |
app.use(bodyParser.urlencoded({ extended: true }));
|
| 12 |
|
| 13 |
-
// Consumer endpoint: front-end must POST { sdp, roomId }
|
| 14 |
app.post("/consumer", async ({ body }, res) => {
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
res.json({ sdp: peer.localDescription });
|
| 33 |
});
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
res.json({ sdp: peer.localDescription });
|
| 52 |
-
});
|
| 53 |
|
| 54 |
-
app.listen(
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const app = express();
|
| 3 |
const bodyParser = require('body-parser');
|
| 4 |
+
const webrtc = require("wrtc");
|
| 5 |
|
| 6 |
+
let senderStream;
|
|
|
|
| 7 |
|
| 8 |
app.use(express.static('public'));
|
| 9 |
app.use(bodyParser.json());
|
| 10 |
app.use(bodyParser.urlencoded({ extended: true }));
|
| 11 |
|
|
|
|
| 12 |
app.post("/consumer", async ({ body }, res) => {
|
| 13 |
+
const peer = new webrtc.RTCPeerConnection({
|
| 14 |
+
iceServers: [
|
| 15 |
+
{
|
| 16 |
+
urls: "stun:stun.stunprotocol.org"
|
| 17 |
+
}
|
| 18 |
+
]
|
| 19 |
+
});
|
| 20 |
+
const desc = new webrtc.RTCSessionDescription(body.sdp);
|
| 21 |
+
await peer.setRemoteDescription(desc);
|
| 22 |
+
senderStream.getTracks().forEach(track => peer.addTrack(track, senderStream));
|
| 23 |
+
const answer = await peer.createAnswer();
|
| 24 |
+
await peer.setLocalDescription(answer);
|
| 25 |
+
const payload = {
|
| 26 |
+
sdp: peer.localDescription
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
res.json(payload);
|
|
|
|
| 30 |
});
|
| 31 |
|
| 32 |
+
app.post('/broadcast', async ({ body }, res) => {
|
| 33 |
+
const peer = new webrtc.RTCPeerConnection({
|
| 34 |
+
iceServers: [
|
| 35 |
+
{
|
| 36 |
+
urls: "stun:stun.stunprotocol.org"
|
| 37 |
+
}
|
| 38 |
+
]
|
| 39 |
+
});
|
| 40 |
+
peer.ontrack = (e) => handleTrackEvent(e, peer);
|
| 41 |
+
const desc = new webrtc.RTCSessionDescription(body.sdp);
|
| 42 |
+
await peer.setRemoteDescription(desc);
|
| 43 |
+
const answer = await peer.createAnswer();
|
| 44 |
+
await peer.setLocalDescription(answer);
|
| 45 |
+
const payload = {
|
| 46 |
+
sdp: peer.localDescription
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
res.json(payload);
|
| 50 |
+
});
|
| 51 |
|
| 52 |
+
function handleTrackEvent(e, peer) {
|
| 53 |
+
senderStream = e.streams[0];
|
| 54 |
+
};
|
| 55 |
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
app.listen(5000, () => console.log('server started'));
|