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

Create public/index.js

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