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])) {
const clients = route['clients']
const headerData = request.headers['x-strapp-type']
- if (route.socket === undefined ) {
- console.log('route socket undefined')
- }
+
+
/* Client is INIT GET */
if (headerData === undefined) {
//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')
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)
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) => {
}
else if (hostMessage['cmd'].localeCompare('< ice pubKey') === 0){
/* if the host response is an ice candidate */
- console.log('Server: Sending host ice candidate')
- let iceCandidateAvailable = hostMessage['iceCandidateAvailable']
+ console.log('Server: Handling host ICE message')
+ let iceState = hostMessage['iceState']
/* If there are any ice candidates, send them back */
- if (iceCandidateAvailable) {
- response.writeHead('200', {'x-strapp-type': 'ice-candidate-available'})
- response.write(JSON.stringify(hostMessage))
- response.end()
- }
- else { /* If not, srequest processed successfully, but there isnt anything yet*/
- console.log('Server: No ice candidate available for response')
- response.writeHead('204', {'x-strapp-type': 'ice-candidate-unavailable'})
- response.end()
- }
- }
- else {
- console.log('unhandled message cmd from host')
+ switch(iceState) {
+ case "a":
+ response.writeHead('200', {'x-strapp-type': 'ice-candidate-available'})
+ response.write(JSON.stringify(hostMessage))
+ response.end()
+ break
+ case "g":
+ console.log('Server: Host is still gathering candidates, keep trying')
+ response.writeHead('200', {'x-strapp-type': 'ice-state-gathering'})
+ response.write(JSON.stringify(hostMessage))
+ response.end()
+ break
+ case "c":
+ console.log('Server: Host has completed gathering candidates')
+ response.writeHead('200', {'x-strapp-type': 'ice-state-complete'})
+ response.write(JSON.stringify(hostMessage))
+ response.end()
+ break
+ default:
+ console.log('unhandled iceState from host')
+ break
+ }
}
})