spec/providers/coreIntegration/rtcpeerconnection.integration.src.js
function(payload, callback) {
    var alice, bob, aliceChannel, bobChannel,
      aliceCandidates = [],
      aliceOffer;
    alice = peercon();
    bob = peercon();
    bob.on('ondatachannel', function (msg) {
      bobChannel = datachan(msg.channel);
      bobChannel.setBinaryType('arraybuffer', function () {});
      bobChannel.on('onmessage', function (msg) {
        alice.close();
        bob.close();
        callback(msg);
      });
    });
    bob.on('onicecandidate', function (msg) {
      msg.candidate && alice.addIceCandidate(msg.candidate);
    });
    alice.on('onicecandidate', function (msg) {
      // firefox needs ice candidates all sent before asking for an answer.
      if (!msg.candidate) {
        bob.setRemoteDescription(aliceOffer).then(function () {
          var last;
          while (aliceCandidates.length) {
            last = bob.addIceCandidate(aliceCandidates.shift());
          }
          return last;
        }).then(function () {
          return bob.createAnswer();
        }).then(function (answer) {
          return bob.setLocalDescription(answer).then(function () {return answer; });
        }).then(function (answer) {
          return alice.setRemoteDescription(answer);
        }, function (err) {
          console.error('RTC failed: ', err);
        });
      } else {
        aliceCandidates.push(msg.candidate);
      }
    });
    alice.createDataChannel('channel').then(function (id) {
      aliceChannel = datachan(id);
      aliceChannel.on('onopen', function () {
        if (payload instanceof ArrayBuffer) {
          aliceChannel.sendBuffer(payload);
        } else {
          aliceChannel.send(payload);
        }
      });
      aliceChannel.setBinaryType('arraybuffer').then(function () {
        return alice.createOffer();
      }).then(function (offer) {
        alice.setLocalDescription(offer);
        aliceOffer = offer;
      });
    }, function (err) {
      console.error('RTC failed: ', err);
    });
  }
function(payload, callback) {
    var alice, bob, aliceChannel, bobChannel,
      aliceCandidates = [],
      aliceOffer;
    alice = peercon();
    bob = peercon();
    bob.on('ondatachannel', function (msg) {
      bobChannel = datachan(msg.channel);
      bobChannel.setBinaryType('arraybuffer', function () {});
      bobChannel.on('onmessage', function (msg) {
        alice.close();
        bob.close();
        callback(msg);
      });
    });
    bob.on('onicecandidate', function (msg) {
      msg.candidate && alice.addIceCandidate(msg.candidate);
    });
    alice.on('onicecandidate', function (msg) {
      // firefox needs ice candidates all sent before asking for an answer.
      if (!msg.candidate) {
        bob.setRemoteDescription(aliceOffer).then(function () {
          var last;
          while (aliceCandidates.length) {
            last = bob.addIceCandidate(aliceCandidates.shift());
          }
          return last;
        }).then(function () {
          return bob.createAnswer();
        }).then(function (answer) {
          return bob.setLocalDescription(answer).then(function () {return answer; });
        }).then(function (answer) {
          return alice.setRemoteDescription(answer);
        }, function (err) {
          console.error('RTC failed: ', err);
        });
      } else {
        aliceCandidates.push(msg.candidate);
      }
    });
    alice.createDataChannel('channel').then(function (id) {
      aliceChannel = datachan(id);
      aliceChannel.on('onopen', function () {
        if (payload instanceof ArrayBuffer) {
          aliceChannel.sendBuffer(payload);
        } else {
          aliceChannel.send(payload);
        }
      });
      aliceChannel.setBinaryType('arraybuffer').then(function () {
        return alice.createOffer();
      }).then(function (offer) {
        alice.setLocalDescription(offer);
        aliceOffer = offer;
      });
    }, function (err) {
      console.error('RTC failed: ', err);
    });
  }
function ( payload , callback ) { var alice , bob , aliceChannel , bobChannel , aliceCandidates = [ ] , aliceOffer ; alice = peercon ( ) ; bob = peercon ( ) ; bob . on ( 'ondatachannel' , function ( msg ) { bobChannel = datachan ( msg . channel ) ; bobChannel . setBinaryType ( 'arraybuffer' , function ( ) { } ) ; bobChannel . on ( 'onmessage' , function ( msg ) { alice . close ( ) ; bob . close ( ) ; callback ( msg ) ; } ) ; } ) ; bob . on ( 'onicecandidate' , function ( msg ) { msg . candidate && alice . addIceCandidate ( msg . candidate ) ; } ) ; alice . on ( 'onicecandidate' , function ( msg ) { // firefox needs ice candidates all sent before asking for an answer. if ( ! msg . candidate ) { bob . setRemoteDescription ( aliceOffer ) . then ( function ( ) { var last ; while ( aliceCandidates . length ) { last = bob . addIceCandidate ( aliceCandidates . shift ( ) ) ; } return last ; } ) . then ( function ( ) { return bob . createAnswer ( ) ; } ) . then ( function ( answer ) { return bob . setLocalDescription ( answer ) . then ( function ( ) { return answer ; } ) ; } ) . then ( function ( answer ) { return alice . setRemoteDescription ( answer ) ; } , function ( err ) { console . error ( 'RTC failed: ' , err ) ; } ) ; } else { aliceCandidates . push ( msg . candidate ) ; } } ) ; alice . createDataChannel ( 'channel' ) . then ( function ( id ) { aliceChannel = datachan ( id ) ; aliceChannel . on ( 'onopen' , function ( ) { if ( payload instanceof ArrayBuffer ) { aliceChannel . sendBuffer ( payload ) ; } else { aliceChannel . send ( payload ) ; } } ) ; aliceChannel . setBinaryType ( 'arraybuffer' ) . then ( function ( ) { return alice . createOffer ( ) ; } ) . then ( function ( offer ) { alice . setLocalDescription ( offer ) ; aliceOffer = offer ; } ) ; } , function ( err ) { console . error ( 'RTC failed: ' , err ) ; } ) ; }
Establishes a peerconnection with one datachannel, sends a message on the datachannel and calls back with the message received by the other peer.
Establishes a peerconnection with one datachannel sends a message on the datachannel and calls back with the message received by the other peer .