+
+ /* Handle all messages from host */
+ sock.on('message', (hostMessage) => {
+ hostMessage = JSON.parse(hostMessage)
+ response = newRoute.clients.get(hostMessage['clientPubKey'])
+
+ /* If the host response is a answer */
+ if (hostMessage['cmd'].localeCompare('< sdp pubKey') === 0) {
+ console.log('Server: Sending host answer to client')
+ response.writeHead(200, { 'Content-Type': 'application/json' })
+ response.write(JSON.stringify(hostMessage))
+ response.end()
+ }
+ else if (hostMessage['cmd'].localeCompare('< ice pubKey') === 0){
+ /* if the host response is an ice candidate */
+ console.log('Server: Handling host ICE message')
+ let iceState = hostMessage['iceState']
+ /* If there are any ice candidates, send them back */
+ 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
+ }
+ }
+
+ })