Browse Source

Add basic js implementation

laramackey 5 years ago
parent
commit
17e4def5ee
5 changed files with 155 additions and 1 deletions
  1. 1 1
      .gitignore
  2. 56 0
      web-client/answerScript.js
  3. 13 0
      web-client/answerer.html
  4. 19 0
      web-client/index.html
  5. 66 0
      web-client/script.js

+ 1 - 1
.gitignore

@@ -5,4 +5,4 @@ build/
 *.so
 compile_commands.json
 tests
-
+.DS_Store

+ 56 - 0
web-client/answerScript.js

@@ -0,0 +1,56 @@
+(function (){
+    console.log('Hello');
+    const connectRemote = document.getElementById('connectRemote')
+    connectRemote.addEventListener('click', createRemoteConnection, false)
+})();
+
+function createRemoteConnection() {
+    const remoteDescText = document.getElementById('remoteDesc').value
+    const remoteDescJSON = JSON.parse(remoteDescText);
+    const remoteDesc = {
+        type: "offer",
+        sdp: remoteDescJSON.description 
+    }
+    const remoteCandidate = {
+        candidate: remoteDescJSON.candidate,
+        sdpMid: "0", // Media stream ID for audio
+        sdpMLineIndex: 0 // Something to do with media
+    }
+    const remoteConnection = new RTCPeerConnection();
+    const connectionInfo = {};
+
+    remoteConnection.setRemoteDescription(remoteDesc).then((e) => {
+        console.log(e)
+    });
+    remoteConnection.addIceCandidate(remoteCandidate).then(
+        () => {console.log('yes')}
+    )
+
+    remoteConnection.createAnswer().then((desc) => {
+        remoteConnection.setLocalDescription(desc);
+        console.dir(desc);
+        connectionInfo.description = desc.sdp;
+    })
+
+
+    remoteConnection.onicecandidate = e => {
+        if (e.candidate) {
+            const candidate = e.candidate.candidate;
+            connectionInfo.candidate = candidate;
+            document.body.append(JSON.stringify(connectionInfo));
+        }
+    }
+    remoteConnection.onicecandidateerror = err => {
+        console.log(err)
+    }
+    remoteConnection.ondatachannel = (e) => {
+        console.log('onDataChannel')
+        const receiveChannel = e.channel;
+        console.dir(receiveChannel);
+        receiveChannel.onmessage = (msg) => {
+            document.body.append(msg.data);
+        }
+    }
+}
+
+

+ 13 - 0
web-client/answerer.html

@@ -0,0 +1,13 @@
+<!doctype html>
+<html>
+    <head>
+        <title>Web Rtc Answerer</title>
+    </head>
+    <body>
+        <h1>Web Rtc</h1>
+        <textarea id="remoteDesc" placeholder="Enter remote JSON" style="width: 500px; height: 200px"></textarea>
+        <br/>
+        <button id="connectRemote">Connect</button>
+        <script src="/answerScript.js"></script>
+    </body>
+</html>

+ 19 - 0
web-client/index.html

@@ -0,0 +1,19 @@
+<!doctype html>
+<html>
+    <head>
+        <title>Web Rtc Example</title>
+    </head>
+    <body>
+        <h1>Web Rtc</h1>
+        <button id="createConnectionBtn">Create Connection</button>
+        <br/>
+        <textarea id="remoteDesc" placeholder="Enter remote JSON" style="width: 500px; height: 200px"></textarea>
+        <br/>
+        <button id="connectRemote">Connect</button>
+        <br/>
+        <textarea id="sendData" placeholder="Send a message" style="width: 500px; height: 50px"></textarea>
+        <br/>
+        <button id="sendDataBtn">Send Data</button>
+        <script src="/script.js"></script>
+    </body>
+</html>

+ 66 - 0
web-client/script.js

@@ -0,0 +1,66 @@
+(function (){
+    console.log('Hello');
+    const connectButton = document.getElementById('createConnectionBtn')
+    connectButton.addEventListener('click', createConnection, false)
+})();
+
+function createConnection() {
+    const localConnection = new RTCPeerConnection();
+    const sendChannel = localConnection.createDataChannel('channel');
+    console.dir(sendChannel);
+    sendChannel.onopen = e => {
+        console.log('open')
+        console.log(e)
+    }
+    sendChannel.onclose = e => {
+        console.log('close')
+        console.log(e)
+    }
+    const connectionInfo = {};
+    localConnection.onicecandidate = e => {
+        if (e.candidate) {
+            const candidate = e.candidate.candidate;
+            connectionInfo.candidate = candidate;
+            document.body.append(JSON.stringify(connectionInfo));
+        }
+    }
+    localConnection.onicecandidateerror = err => {
+        console.log(err)
+    }
+    
+    localConnection.createOffer().then((desc) => {
+            connectionInfo.description = desc.sdp;
+            localConnection.setLocalDescription(desc);
+        }
+    )
+    const connectRemote = document.getElementById('connectRemote')
+
+    function createRemoteConnection() {
+        const remoteDescText = document.getElementById('remoteDesc').value
+        const remoteDescJSON = JSON.parse(remoteDescText);
+        const remoteDesc = {
+            type: "answer",
+            sdp: remoteDescJSON.description 
+        }
+        const remoteCandidate = {
+            candidate: remoteDescJSON.candidate,
+            sdpMid: "0", // Media stream ID for audio
+            sdpMLineIndex: 0 // Something to do with media
+        }
+        console.dir(remoteDescJSON)
+        localConnection.setRemoteDescription(remoteDesc).then((e) => {
+            console.log(e)
+        });
+    }
+
+    connectRemote.addEventListener('click', createRemoteConnection, false)
+
+    const sendButton = document.getElementById('sendDataBtn')
+    sendButton.addEventListener('click', sendMessage, false)
+    function sendMessage() {
+        const messageText = document.getElementById('sendData').value;
+        sendChannel.send(messageText);   
+    }
+}
+
+