- /* Poll for answer */
- return pollServer(window.location, offer, pollServerTimeout)
- }).then((serverResponse) => {
- const answer = JSON.parse(serverResponse)
- console.log(answer)
- /* TODO: State machine to parse answer */
- console.log('Setting Remote Description')
- cpc.setRemoteDescription(answer.sdp)
- }).catch( (err) => {
- console.log('error in sdp handshake: ' + err)
- })
-}).catch((err) => {
- console.log(err)
+/* Create, set, and get client Offer. Poll server for host answer.
+ Set host answer as client remoteDescription */
+getPublicKey().then((cpk) => {
+ const cpc = new RTCPeerConnection(conf)
+ /* Start data channel */
+ sendChannel = cpc.createDataChannel("sendChannel");
+ sendChannel.onopen = () => {
+ console.log('client data channel on line')
+ sendChannel.onmessage = (message) => {
+ console.log(message.data)
+ }
+ sendChannel.send('Hi from the Client')
+ };
+ /* Start polling for ice candidate */
+
+
+ console.log(cpc.iceConnectionState)
+ cpc.oniceconnectionstatechange = () => {
+ console.log('iceConnectionState = ' + cpc.iceConnectionState)
+ }
+
+
+
+ cpc.onnegotiationneeded = () => {
+ cpc.createOffer().then((offer) => {
+ return cpc.setLocalDescription(offer)
+ })
+ .then(() => {
+ console.log('Client: Sending offer to host')
+ let offer = {
+ cmd: '> sdp pubKey',
+ sdp: cpc.localDescription,
+ pubKey: cpk.n
+ }
+ return pollServer(window.location, offer, pollServerForAnswer)
+ }).then((serverResponse) => {
+ const answer = JSON.parse(serverResponse)
+ console.log('Client: received host answer')
+ cpc.setRemoteDescription(answer.sdp).then(() => {
+ console.log('Client: Polling for ICE candidates')
+ pollServerForICECandidate(cpc)
+ })
+ cpc.onicecandidate = (event) => {
+ if (event.candidate) {
+ console.log('Client: Sending ice candidate to host')
+ postServer(window.location, JSON.stringify({
+ cmd: '> ice pubkey',
+ ice: event.candidate,
+ pubKey: cpk.n
+ }))
+ }
+ else {
+ console.log('Client: No more Ice Candidates to send')
+ }
+ }
+ }).catch( (err) => {
+ console.log('error in sdp handshake: ' + err)
+ })
+ }