Pepguy commited on
Commit
4c32ad1
·
verified ·
1 Parent(s): 0ed692f

Create public/viewer.js

Browse files
Files changed (1) hide show
  1. public/viewer.js +41 -0
public/viewer.js ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ window.onload = () => {
2
+ document.getElementById('my-button').onclick = () => {
3
+ init();
4
+ }
5
+ }
6
+
7
+ async function init() {
8
+ const peer = createPeer();
9
+ peer.addTransceiver("video", { direction: "recvonly" })
10
+ }
11
+
12
+ function createPeer() {
13
+ const peer = new RTCPeerConnection({
14
+ iceServers: [
15
+ {
16
+ urls: "stun:stun.stunprotocol.org"
17
+ }
18
+ ]
19
+ });
20
+ peer.ontrack = handleTrackEvent;
21
+ peer.onnegotiationneeded = () => handleNegotiationNeededEvent(peer);
22
+
23
+ return peer;
24
+ }
25
+
26
+ async function handleNegotiationNeededEvent(peer) {
27
+ const offer = await peer.createOffer();
28
+ await peer.setLocalDescription(offer);
29
+ const payload = {
30
+ sdp: peer.localDescription,
31
+ roomId: "gods"
32
+ };
33
+
34
+ const { data } = await axios.post('/consumer', payload);
35
+ const desc = new RTCSessionDescription(data.sdp);
36
+ peer.setRemoteDescription(desc).catch(e => console.log(e));
37
+ }
38
+
39
+ function handleTrackEvent(e) {
40
+ document.getElementById("video").srcObject = e.streams[0];
41
+ };