X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=main.js;h=f123a413fd73593d94bbbce8705586395cac3c90;hp=75e762f7f231c92a5431310c5d82890f8fd27a56;hb=d33866cf8b1a188ad9ab23a934986bfd1b4726cb;hpb=8a767de924e57ea87bc2d0d0b9cf9864e523d916 diff --git a/main.js b/main.js index 75e762f..f123a41 100644 --- a/main.js +++ b/main.js @@ -25,6 +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') const serveFile = (fPath) => { fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => { if (err || data == undefined) { @@ -42,7 +52,7 @@ const router = { let routePath = htArgv[0].split('/') let routeName = routePath[0] 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])) { @@ -57,14 +67,27 @@ const router = { console.log(`SEC: ${localPath} references files not in route`) } } + /* TODO: Handle reconnecting host */ else if (routeName in router.routes) { + const route = router.routes[routeName] - response.writeHead(200, { 'Content-Type': 'text/html' }) - response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`) - response.end() - //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) - route.socket.send(request.headers['x-forwarded-for'] || request.connection.remoteAddress) + + /* Client is INIT GET */ + if (request.headers['x-strapp-type'] == undefined) { + console.log('client init GET') + response.writeHead(200, { 'Content-Type': 'text/html' }) + response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`) + response.end() + //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 { /* Client sent offer, waiting for answer */ + console.log(JSON.parse(request.headers['x-strapp-type'])) + route.socket.on('message', (hostResponse) => { + console.log(hostResponse) + }) + } + } else { router.routes[routeName] = true @@ -101,6 +124,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