-const cpc = new RTCPeerConnection()
-cpc.createOffer().then((offer) => {
- console.log('creating offer which is ' + offer)
- return cpc.setLocalDescription(offer)
-}).then(() => {
- console.log('sessionDescriptionInit = ' + cpc.localDescription)
- const cpk = getPublicKey()
- let offer = {
- cmd: '> sdp pubKey'
- sdp: cpc.localDescription,
- pubKey: cpk
+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)
+ })