working on client-server communication
[henge/kiak.git] / main.js
diff --git a/main.js b/main.js
index 4ada8bb..845e54b 100644 (file)
--- a/main.js
+++ b/main.js
@@ -25,8 +25,16 @@ const router = {
   httpd:      undefined,
   wsProtocol: opts['no-tls'] ? 'ws' : 'wss',
   respond:    (request,response) => {
+    let body = []
+    request.on('error', function(err) {
+      console.error(`error is ${err}`);
+    }).on('data', function(chunk) {
+      console.log(`chunk is ${chunk}`)
+      body.push(chunk);
+    }).on('end', function() {
+      console.log(`body is ${body}`)
+    })
     console.log('server handling request')
-    console.log(request)
     const serveFile = (fPath) => {
       fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => {
         if (err || data == undefined) {
@@ -61,10 +69,12 @@ const router = {
     }
     /* TODO: Handle reconnecting host */
     else if (routeName in router.routes) {
+
       const route = router.routes[routeName]
-      console.log(request)
+
       /* Client is INIT GET */
-      if (request) {
+      if (request.headers['x-strapp-type'] !== 'o') {
+        console.log('client init GET')
         response.writeHead(200, { 'Content-Type': 'text/html' })
         response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`)
         response.end()
@@ -72,7 +82,11 @@ const router = {
         //      (this happens when a client connects to an active route with no currently-online host)
       }
       else { /* Client sent offer, waiting for answer */
-
+        console.log('client offer/answer GET')
+        //route.socket.send(JSON.parse(body.join('')))
+        route.socket.on('message', (hostResponse) => {
+          console.log(hostResponse)
+        })
       }
 
     }
@@ -111,6 +125,7 @@ const router = {
   * @summary Boot up the router.  With TLS, we must wait for file reads to sync.
   */
   if (!opts['no-tls']) {
+    console.log('tls')
     let filesRead = 0
     let key = undefined
     let cert = undefined