- 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)
+ const clients = route['clients']
+ const headerData = request.headers['x-strapp-type']
+
+
+
+
+ /* Client is INIT GET */
+ if (headerData === 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 if (headerData.localeCompare('ice-candidate-request') === 0) {
+ console.log('Server: received ice-candidate-request from Client')
+ 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')
+ let data = []
+ request.on('data', (chunk) => {
+ data.push(chunk)
+ }).on('end', () => {
+ console.log('Sending ice-candidate-submission to Host')
+ data = Buffer.concat(data).toString();
+ clients.set(JSON.parse(data)['pubKey'], response)
+ route.socket.send(data)
+ })
+ }
+ else if (headerData.localeCompare('client-sdp-offer') === 0){ /* Client sent offer, waiting for answer */
+ console.log('Server: Sending client offer to host')
+ clients.set(JSON.parse(request.headers['x-client-offer'])['pubKey'], response)
+ route.socket.send(request.headers['x-client-offer'])
+ } else {
+ console.log('Unhandled stuff')
+ console.log(request.headers)
+ }
+