]> git.r.bdr.sh - rbdr/junction/blobdiff - extension/content_script.js
Fix server
[rbdr/junction] / extension / content_script.js
index 279c52de5adad64e34da41a00eb78d05d235c7d8..6785a6556427ab48b49495eda7f51fc907717465 100644 (file)
@@ -4,11 +4,14 @@ import Media from './media';
 
 const internals = {
 
 
 const internals = {
 
-  kSocketUrl: 'ws://localhost:8000',
+  kSocketUrl: 'https://junction.tranquil.services',
+       kIceServers: [
+               {url:"stun:stun.l.google.com:19302"}
+       ],
 
   port: null,
   socket: null,
 
   port: null,
   socket: null,
-  peers: 0,
+  peers: {},
 
   onMessage(message) {
     internals[message.action](message.data);
 
   onMessage(message) {
     internals[message.action](message.data);
@@ -19,7 +22,7 @@ const internals = {
     internals.tada = data.tada; // Keeping for fun
 
     try {
     internals.tada = data.tada; // Keeping for fun
 
     try {
-      await Media.start();
+      const mediaStream = await Media.start();
 
       internals.socket = io(internals.kSocketUrl, {
         transports: ['websocket']
 
       internals.socket = io(internals.kSocketUrl, {
         transports: ['websocket']
@@ -51,7 +54,52 @@ const internals = {
       internals.socket.on('addPeer', function(data) {
 
         console.log(data);
       internals.socket.on('addPeer', function(data) {
 
         console.log(data);
-        Peers.add('id-'+Peers.count(), internals.tada);
+        Peers.add(data.peer_id, internals.tada);
+          const peerId = data.peer_id;
+
+          const peerConn = new RTCPeerConnection(
+              {"iceServers": internals.kIceServers},
+              {"optional": [{"DtlsSrtpKeyAgreement": true}]}
+          );
+
+          internals.peers[peerId] = peerConn;
+
+          peerConn.onicecandidate = (event) => {
+            if (event.candidate) {
+              internals.socket.emit('relayICECandidate', {
+                'peer_id': peerId,
+                'ice_candidate': {
+                  'sdpMLineIndex': event.candidate.sdpMLineIndex,
+                  'candidate': event.candidate.candidate
+                }
+              });
+            }
+          }
+
+          peerConn.onaddstream = (stream) => {
+            console.log(`Received stream for peer ${peerId}`);
+            console.log(stream);
+          }
+
+          peerConn.addStream(mediaStream);
+
+          if (data.should_create_offer) {
+            console.log("Creating RTC offer to ", peerId);
+            peerConn.createOffer((local_description) => {
+                  console.log("Local offer description is: ", local_description);
+                  peerConn.setLocalDescription(local_description, () => {
+                        internals.socket.emit('relaySessionDescription', {
+                          'peer_id': peerId,
+                          'session_description': local_description
+                        });
+
+                        console.log("Offer setLocalDescription succeeded");
+                      }, () => { console.log("Offer setLocalDescription failed!"); }
+                  );
+                },
+                (error) => { console.log("Error sending offer: ", error) }
+            );
+          }
         console.log(`There are now ${Peers.count()} participants`);
       });
 
         console.log(`There are now ${Peers.count()} participants`);
       });