Browse Source

string instead of json

laramackey 5 years ago
parent
commit
d7dd7c49df
4 changed files with 23 additions and 28 deletions
  1. 12 6
      web-client/answerScript.js
  2. 3 0
      web-client/script.js
  3. 0 14
      web-client/server.js
  4. 8 8
      webrtc_server/src/main.rs

+ 12 - 6
web-client/answerScript.js

@@ -1,7 +1,10 @@
 (function (){
     const urlParams = new URLSearchParams(window.location.search);
     const connectionParam = urlParams.get('connection')
-    const connection = JSON.parse(atob(connectionParam));
+    console.log(atob(connectionParam));
+    const decoded = atob(connectionParam);
+    const bits = decoded.split('xxxxx');
+    const connection = {description: bits[0], candidate: bits[1]}
     console.dir(connection);
     createRemoteConnection(connection);
 })();
@@ -17,7 +20,7 @@ function createRemoteConnection(remoteDescJSON) {
         sdpMLineIndex: 0 // Something to do with media
     }
     const remoteConnection = new RTCPeerConnection();
-    const connectionInfo = {};
+    let connectionDescription = '';
 
     remoteConnection.setRemoteDescription(remoteDesc).then((e) => {
         console.log(e)
@@ -29,20 +32,19 @@ function createRemoteConnection(remoteDescJSON) {
     remoteConnection.createAnswer().then((desc) => {
         remoteConnection.setLocalDescription(desc);
         console.dir(desc);
-        connectionInfo.description = desc.sdp;
+        connectionDescription = desc.sdp;
     })
 
 
     remoteConnection.onicecandidate = e => {
         if (e.candidate) {
             const candidate = e.candidate.candidate;
-            connectionInfo.candidate = candidate;
-            const body = JSON.stringify(connectionInfo)
+            const body = connectionDescription;
             fetch(
                 'http://localhost:8000/state/json',
                 {
                     method: 'post',
-                    headers: {'Content-Type': 'application/json'},
+                    headers: {'Content-Type': 'text/plain'},
                     body
                 }
             ).then(response => {
@@ -59,6 +61,10 @@ function createRemoteConnection(remoteDescJSON) {
         console.log('onDataChannel')
         const receiveChannel = e.channel;
         console.dir(receiveChannel);
+        receiveChannel.onopen = () => {
+            console.log('channel open')
+            receiveChannel.send('testing testing 123'); 
+        }
         receiveChannel.onmessage = (msg) => {
             document.body.append(msg.data);
         }

+ 3 - 0
web-client/script.js

@@ -74,6 +74,9 @@ function createConnection() {
         const messageText = document.getElementById('sendData').value;
         sendChannel.send(messageText);   
     }
+    sendChannel.onmessage = (msg) => {
+        document.body.append(msg.data);
+    }
 }
 
 

+ 0 - 14
web-client/server.js

@@ -1,14 +0,0 @@
-const http = require('http');
-
-
-
-const connection = {
-    "description":"v=0\r\no=- 9134460598269614011 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0\r\na=msid-semantic: WMS\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\nc=IN IP4 0.0.0.0\r\na=ice-ufrag:xlQY\r\na=ice-pwd:STMZXrUH+JPpmy86lz5Tp0YB\r\na=ice-options:trickle\r\na=fingerprint:sha-256 80:4C:CB:B5:45:89:0C:51:0A:2A:E5:AC:96:A7:53:F3:42:2B:F6:B8:1D:B0:CE:44:9F:0B:86:FC:B0:BD:94:F7\r\na=setup:actpass\r\na=mid:0\r\na=sctp-port:5000\r\na=max-message-size:262144\r\n",
-    "candidate":"candidate:269456666 1 udp 2113937151 10.0.1.185 58326 typ host generation 0 ufrag xlQY network-cost 999"
-};
-
-console.log('http://localhost:8000/answerer.html?connection=' + Buffer.from(JSON.stringify(connection)).toString('base64'));
-http.createServer(function(req, res){
-    res.writeHead(200, {'Content-Type': 'application/json'});
-    res.end(JSON.stringify());
-}).listen(3030);

+ 8 - 8
webrtc_server/src/main.rs

@@ -46,7 +46,7 @@ fn make_cors() -> Cors {
     .expect("error while building CORS")
 }
 
-type Data = Mutex<ChannelData>;
+type Data = Mutex<String>;
 
 #[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
 pub struct ChannelData {
@@ -63,17 +63,17 @@ impl Default for ChannelData {
     }
 }
 
-#[post("/json", format = "json", data = "<payload>")]
-fn new(payload: Json<ChannelData>, state: rocket::State<Data>) -> JsonValue {
+#[post("/json", data = "<payload>")]
+fn new(payload: String, state: rocket::State<Data>) -> JsonValue {
     let mut data = state.lock().expect("state locked");
-    *data = payload.into_inner();
+    *data = payload;
     json!({ "status": "ok" })
 }
 
-#[get("/json", format = "json")]
-fn get(state: rocket::State<Data>) -> Option<Json<ChannelData>> {
+#[get("/json")]
+fn get(state: rocket::State<Data>) -> Option<String> {
     let data = state.lock().expect("state locked");
-    Some(Json(data.clone()))
+    Some(data.clone())
 }
 
 #[catch(404)]
@@ -88,7 +88,7 @@ fn rocket() -> rocket::Rocket {
     rocket::ignite()
         .mount("/state", routes![new, get])
         .register(catchers![not_found])
-        .manage(Mutex::new(ChannelData::default()))
+        .manage(Mutex::new(String::new()))
         .attach(make_cors())
 }