function Peer(opts) {
    _classCallCheck(this, Peer);

    var _this = _possibleConstructorReturn(this, (Peer.__proto__ || Object.getPrototypeOf(Peer)).call(this));

    _this._id = (0, _uuid2.default)().substr(0, 8);
    // self._debug('new peer %o', opts)

    _this.opts = Object.assign({
      allowHalfOpen: false,
      debug: false
    }, opts);

    _this.channelName = _this.opts.initiator ? _this.opts.channelName || (0, _uuid2.default)() : null;

    _this.initiator = _this.opts.initiator || false;
    _this.channelConfig = _this.opts.channelConfig || Peer.channelConfig;
    _this.config = _this.opts.config || Peer.config;
    _this.constraints = Peer.transformConstraints(_this.opts.constraints || Peer.constraints);
    _this.offerConstraints = Peer.transformConstraints(_this.opts.offerConstraints || {});
    _this.answerConstraints = Peer.transformConstraints(_this.opts.answerConstraints || {});
    _this.reconnectTimer = _this.opts.reconnectTimer || false;
    _this.sdpTransform = _this.opts.sdpTransform || function (sdp) {
      return sdp;
    };
    _this.stream = _this.opts.stream || false;
    _this.trickle = _this.opts.trickle !== undefined ? _this.opts.trickle : true;
    _this._earlyMessage = null;

    _this.destroyed = false;
    _this.connected = false;

    _this.remoteAddress = undefined;
    _this.remoteFamily = undefined;
    _this.remotePort = undefined;
    _this.localAddress = undefined;
    _this.localPort = undefined;

    _this._wrtc = _this.opts.wrtc && _typeof(_this.opts.wrtc) === 'object' ? _this.opts.wrtc : (0, _getBrowserRtc2.default)();

    if (!_this._wrtc) {
      throw new Error('No WebRTC support: Not a supported browser');
    }

    _this._pcReady = false;
    _this._channelReady = false;
    _this._iceComplete = false; // ice candidate trickle done (got null candidate)
    _this._channel = null;
    _this._pendingCandidates = [];
    _this._previousStreams = [];

    _this._chunk = null;
    _this._cb = null;
    _this._interval = null;
    _this._reconnectTimeout = null;

    _this._pc = new _this._wrtc.RTCPeerConnection(_this.config, _this.constraints);

    _this._pc.oniceconnectionstatechange = function () {
      _this._onIceStateChange();
    };
    _this._pc.onicegatheringstatechange = function () {
      _this._onIceStateChange();
    };
    _this._pc.onsignalingstatechange = function () {
      _this._onSignalingStateChange();
    };
    _this._pc.onicecandidate = function (event) {
      _this._onIceCandidate(event);
    };

    // Other spec events, unused by this implementation:
    // - onconnectionstatechange
    // - onicecandidateerror
    // - onfingerprintfailure

    if (_this.initiator) {
      var createdOffer = false;
      _this._pc.onnegotiationneeded = function () {
        if (!createdOffer) _this._createOffer();
        createdOffer = true;
      };

      _this._setupData({
        channel: _this._pc.createDataChannel(_this.channelName, _this.channelConfig)
      });
    } else {
      _this._pc.ondatachannel = function (event) {
        _this._setupData(event);
      };
    }

    if ('addTrack' in _this._pc) {
      _this._pc.ontrack = function (event) {
        _this._onTrack(event);
      };
    } else {
      // This can be removed once all browsers support `ontrack`
      // Further reading: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream
      _this._pc.onaddstram = function (event) {
        _this._onAddStram(event);
      };
    }
    // HACK: wrtc doesn't fire the 'negotionneeded' event
    if (_this.initiator && _this._isWrtc) {
      _this._pc.onnegotiationneeded();
    }

    _this._onFinishBound = function () {
      _this._onFinish();
    };
    _this.once('finish', _this._onFinishBound);
    return _this;
  } // END CONSTRUCTOR
/**
   * Creates a new P2P connection and sets up internal WebRTC events.
   * @param {PeerOptions} opts
   */
Creates a new P2P connection and sets up internal WebRTC events.
Creates a new P2P connection and sets up internal WebRTC events .
function Peer(opts) {
    _classCallCheck(this, Peer);
    var _this = _possibleConstructorReturn(this, (Peer.__proto__ || Object.getPrototypeOf(Peer)).call(this));
    _this._id = (0, _uuid2.default)().substr(0, 8);
    _this.opts = Object.assign({
      allowHalfOpen: false,
      debug: false
    }, opts);
    _this.channelName = _this.opts.initiator ? _this.opts.channelName || (0, _uuid2.default)() : null;
    _this.initiator = _this.opts.initiator || false;
    _this.channelConfig = _this.opts.channelConfig || Peer.channelConfig;
    _this.config = _this.opts.config || Peer.config;
    _this.constraints = Peer.transformConstraints(_this.opts.constraints || Peer.constraints);
    _this.offerConstraints = Peer.transformConstraints(_this.opts.offerConstraints || {});
    _this.answerConstraints = Peer.transformConstraints(_this.opts.answerConstraints || {});
    _this.reconnectTimer = _this.opts.reconnectTimer || false;
    _this.sdpTransform = _this.opts.sdpTransform || function (sdp) {
      return sdp;
    };
    _this.stream = _this.opts.stream || false;
    _this.trickle = _this.opts.trickle !== undefined ? _this.opts.trickle : true;
    _this._earlyMessage = null;
    _this.destroyed = false;
    _this.connected = false;
    _this.remoteAddress = undefined;
    _this.remoteFamily = undefined;
    _this.remotePort = undefined;
    _this.localAddress = undefined;
    _this.localPort = undefined;
    _this._wrtc = _this.opts.wrtc && _typeof(_this.opts.wrtc) === 'object' ? _this.opts.wrtc : (0, _getBrowserRtc2.default)();
    if (!_this._wrtc) {
      throw new Error('No WebRTC support: Not a supported browser');
    }
    _this._pcReady = false;
    _this._channelReady = false;
    _this._iceComplete = false; 
    _this._channel = null;
    _this._pendingCandidates = [];
    _this._previousStreams = [];
    _this._chunk = null;
    _this._cb = null;
    _this._interval = null;
    _this._reconnectTimeout = null;
    _this._pc = new _this._wrtc.RTCPeerConnection(_this.config, _this.constraints);
    _this._pc.oniceconnectionstatechange = function () {
      _this._onIceStateChange();
    };
    _this._pc.onicegatheringstatechange = function () {
      _this._onIceStateChange();
    };
    _this._pc.onsignalingstatechange = function () {
      _this._onSignalingStateChange();
    };
    _this._pc.onicecandidate = function (event) {
      _this._onIceCandidate(event);
    };
    if (_this.initiator) {
      var createdOffer = false;
      _this._pc.onnegotiationneeded = function () {
        if (!createdOffer) _this._createOffer();
        createdOffer = true;
      };
      _this._setupData({
        channel: _this._pc.createDataChannel(_this.channelName, _this.channelConfig)
      });
    } else {
      _this._pc.ondatachannel = function (event) {
        _this._setupData(event);
      };
    }
    if ('addTrack' in _this._pc) {
      _this._pc.ontrack = function (event) {
        _this._onTrack(event);
      };
    } else {
      _this._pc.onaddstram = function (event) {
        _this._onAddStram(event);
      };
    }
    if (_this.initiator && _this._isWrtc) {
      _this._pc.onnegotiationneeded();
    }
    _this._onFinishBound = function () {
      _this._onFinish();
    };
    _this.once('finish', _this._onFinishBound);
    return _this;
  } 
function Peer ( opts ) { _classCallCheck ( this , Peer ) ; var _this = _possibleConstructorReturn ( this , ( Peer . __proto__ || Object . getPrototypeOf ( Peer ) ) . call ( this ) ) ; _this . _id = ( 0 , _uuid2 . default ) ( ) . substr ( 0 , 8 ) ; _this . opts = Object . assign ( { allowHalfOpen : false , debug : false } , opts ) ; _this . channelName = _this . opts . initiator ? _this . opts . channelName || ( 0 , _uuid2 . default ) ( ) : null ; _this . initiator = _this . opts . initiator || false ; _this . channelConfig = _this . opts . channelConfig || Peer . channelConfig ; _this . config = _this . opts . config || Peer . config ; _this . constraints = Peer . transformConstraints ( _this . opts . constraints || Peer . constraints ) ; _this . offerConstraints = Peer . transformConstraints ( _this . opts . offerConstraints || { } ) ; _this . answerConstraints = Peer . transformConstraints ( _this . opts . answerConstraints || { } ) ; _this . reconnectTimer = _this . opts . reconnectTimer || false ; _this . sdpTransform = _this . opts . sdpTransform || function ( sdp ) { return sdp ; } ; _this . stream = _this . opts . stream || false ; _this . trickle = _this . opts . trickle !== undefined ? _this . opts . trickle : true ; _this . _earlyMessage = null ; _this . destroyed = false ; _this . connected = false ; _this . remoteAddress = undefined ; _this . remoteFamily = undefined ; _this . remotePort = undefined ; _this . localAddress = undefined ; _this . localPort = undefined ; _this . _wrtc = _this . opts . wrtc && _typeof ( _this . opts . wrtc ) === 'object' ? _this . opts . wrtc : ( 0 , _getBrowserRtc2 . default ) ( ) ; if ( ! _this . _wrtc ) { throw new Error ( 'No WebRTC support: Not a supported browser' ) ; } _this . _pcReady = false ; _this . _channelReady = false ; _this . _iceComplete = false ; _this . _channel = null ; _this . _pendingCandidates = [ ] ; _this . _previousStreams = [ ] ; _this . _chunk = null ; _this . _cb = null ; _this . _interval = null ; _this . _reconnectTimeout = null ; _this . _pc = new _this . _wrtc . RTCPeerConnection ( _this . config , _this . constraints ) ; _this . _pc . oniceconnectionstatechange = function ( ) { _this . _onIceStateChange ( ) ; } ; _this . _pc . onicegatheringstatechange = function ( ) { _this . _onIceStateChange ( ) ; } ; _this . _pc . onsignalingstatechange = function ( ) { _this . _onSignalingStateChange ( ) ; } ; _this . _pc . onicecandidate = function ( event ) { _this . _onIceCandidate ( event ) ; } ; if ( _this . initiator ) { var createdOffer = false ; _this . _pc . onnegotiationneeded = function ( ) { if ( ! createdOffer ) _this . _createOffer ( ) ; createdOffer = true ; } ; _this . _setupData ( { channel : _this . _pc . createDataChannel ( _this . channelName , _this . channelConfig ) } ) ; } else { _this . _pc . ondatachannel = function ( event ) { _this . _setupData ( event ) ; } ; } if ( 'addTrack' in _this . _pc ) { _this . _pc . ontrack = function ( event ) { _this . _onTrack ( event ) ; } ; } else { _this . _pc . onaddstram = function ( event ) { _this . _onAddStram ( event ) ; } ; } if ( _this . initiator && _this . _isWrtc ) { _this . _pc . onnegotiationneeded ( ) ; } _this . _onFinishBound = function ( ) { _this . _onFinish ( ) ; } ; _this . once ( 'finish' , _this . _onFinishBound ) ; return _this ; }
Creates a new P2P connection and sets up internal WebRTC events.
Creates a new P2P connection and sets up internal WebRTC events .
// self._debug('new peer %o', opts) // ice candidate trickle done (got null candidate) // Other spec events, unused by this implementation: // - onconnectionstatechange // - onicecandidateerror // - onfingerprintfailure // This can be removed once all browsers support `ontrack` // Further reading: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream // HACK: wrtc doesn't fire the 'negotionneeded' event // END CONSTRUCTOR