data channel between host + client, sending/receiving ice candidates when needed...
[henge/kiak.git] / main.js
diff --git a/main.js b/main.js
index a5b105b..9ef0b49 100644 (file)
--- a/main.js
+++ b/main.js
@@ -45,7 +45,7 @@ const router = {
 
 
     if (routeName === '' || routeName === 'index.html')
-      serveFile(opts['index'])
+    serveFile(opts['index'])
     else if (routeName in opts['bindings']) {
       let localPath = path.normalize(opts['bindings'][routeName].concat(path.sep + routePath.slice(1).join(path.sep)))
       if (localPath.includes(opts['bindings'][routeName])) {
@@ -68,6 +68,7 @@ const router = {
 
 
 
+
       /* Client is INIT GET */
       if (headerData === undefined) {
         console.log('client init GET')
@@ -77,17 +78,12 @@ const router = {
         //TODO: if route.socket == undefined: have server delay this send until host connects
         //      (this happens when a client connects to an active route with no currently-online host)
       }
-      else if (headerData.localeCompare('ice-candidate-request') === 0){
+      else if (headerData.localeCompare('ice-candidate-request') === 0) {
         console.log('Server: received ice-candidate-request from Client')
-        let data = []
-        request.on('data', (chunk) => {
-          data.push(chunk)
-        }).on('end', () => {
-          data = Buffer.concat(data).toString();
-          console.log('Sending ice-candidate-request to Host' + data)
-          clients.set(data, response)
-          route.socket.send(data)
-        })
+        let pubKey = request.headers['x-client-pubkey']
+        clients.set(pubKey, response)
+        pubKey = '{ "pubKey": "'  + pubKey + '" }'
+        route.socket.send(pubKey)
       }
       else if (headerData.localeCompare('ice-candidate-submission') === 0) {
         console.log('Server: recieved ice-candidate-submission from Client')
@@ -95,7 +91,7 @@ const router = {
         request.on('data', (chunk) => {
           data.push(chunk)
         }).on('end', () => {
-          console.log('Sending ice-candidate-submission to Host' + data)
+          console.log('Sending ice-candidate-submission to Host')
           data = Buffer.concat(data).toString();
           clients.set(JSON.parse(data)['pubKey'], response)
           route.socket.send(data)
@@ -119,9 +115,9 @@ const router = {
       getport().then( (port) => {
         newRoute.port = port
         if (opts['no-tls'])
-          newRoute.httpd = http.createServer()
+        newRoute.httpd = http.createServer()
         else
-          newRoute.httpd = https.createServer(router.httpsOpts)
+        newRoute.httpd = https.createServer(router.httpsOpts)
         newRoute.httpd.listen(newRoute.port)
         newRoute.wsd = new ws.Server( { server: newRoute.httpd } )
         newRoute.wsd.on('connection', (sock) => {