コード例 #1
0
ファイル: index.js プロジェクト: contolini/rtc-quickconnect
    generateIceServers(extend({targetPeer: id}, opts, (scheme || {}).connection), function(err, iceServers) {
      if (err) {
        signaller('icegeneration:error', id, scheme && scheme.id, err);
      } else {
        signaller('peer:iceservers', id, scheme && scheme.id, iceServers || []);
      }

      // create a peer connection
      // iceServers that have been created using genice taking precendence
      pc = rtc.createConnection(
        extend({}, opts, { iceServers: iceServers }),
        (opts || {}).constraints
      );

      signaller('peer:connect', id, pc, data);

      // add this connection to the calls list
      call = calls.create(id, pc, data);

      // add the local streams
      localStreams.forEach(function(stream) {
        pc.addStream(stream);
      });

      // add the data channels
      // do this differently based on whether the connection is a
      // master or a slave connection
      if (signaller.isMaster(id)) {
        debug('is master, creating data channels: ', Object.keys(channels));

        // create the channels
        Object.keys(channels).forEach(function(label) {
         gotPeerChannel(pc.createDataChannel(label, channels[label]), pc, data);
        });
      }
      else {
        pc.ondatachannel = function(evt) {
          var channel = evt && evt.channel;

          // if we have no channel, abort
          if (! channel) {
            return;
          }

          if (channels[channel.label] !== undefined) {
            gotPeerChannel(channel, pc, getPeerData(id));
          }
        };
      }

      // couple the connections
      debug('coupling ' + signaller.id + ' to ' + id);
      monitor = rtc.couple(pc, id, signaller, extend({}, opts, {
        logger: mbus('pc.' + id, signaller)
      }));

      // Apply the monitor to the call
      call.monitor = monitor;

      // once active, trigger the peer connect event
      monitor.once('connected', function() {
        clearPending('connected successfully');
        calls.start(id, pc, data);
      });
      monitor.once('closed', function() {
        clearPending('closed');
        calls.end(id);
      });
      monitor.once('aborted', function() {
        clearPending('aborted');
      });
      monitor.once('failed', function() {
        clearPending('failed');
        calls.fail(id);
      });

      // The following states are intermediate states based on the disconnection timer
      monitor.once('failing', calls.failing.bind(null, id));
      monitor.once('recovered', calls.recovered.bind(null, id));

      // Fire the couple event
      signaller('peer:couple', id, pc, data, monitor);

      // if we are the master connnection, create the offer
      // NOTE: this only really for the sake of politeness, as rtc couple
      // implementation handles the slave attempting to create an offer
      if (signaller.isMaster(id)) {
        monitor.createOffer();
      }

      signaller('peer:prepared', id);
    });
コード例 #2
0
ファイル: index.js プロジェクト: hfeeki/rtc-quickconnect
  function handlePeerAnnounce(data) {
    var pc;
    var monitor;

    // if the room is not a match, abort
    if (data.room !== room) {
      return;
    }

    // create a peer connection
    pc = rtc.createConnection(opts, (opts || {}).constraints);
    signaller.emit('peer:connect', data.id, pc, data);

    // add this connection to the calls list
    callCreate(data.id, pc, data);

    // add the local streams
    localStreams.forEach(function(stream, idx) {
      pc.addStream(stream);
    });

    // add the data channels
    // do this differently based on whether the connection is a
    // master or a slave connection
    if (signaller.isMaster(data.id)) {
      debug('is master, creating data channels: ', Object.keys(channels));

      // create the channels
      Object.keys(channels).forEach(function(label) {
       gotPeerChannel(pc.createDataChannel(label, channels[label]), pc, data);
      });
    }
    else {
      pc.ondatachannel = function(evt) {
        var channel = evt && evt.channel;

        // if we have no channel, abort
        if (! channel) {
          return;
        }

        if (channels[channel.label] !== undefined) {
          gotPeerChannel(channel, pc, data);
        }
      };
    }

    // couple the connections
    debug('coupling ' + signaller.id + ' to ' + data.id);
    monitor = rtc.couple(pc, data.id, signaller, opts);
    signaller.emit('peer:couple', data.id, pc, data, monitor);

    // once active, trigger the peer connect event
    monitor.once('connected', callStart.bind(null, data.id, pc, data))
    monitor.once('closed', callEnd.bind(null, data.id));

    // if we are the master connnection, create the offer
    // NOTE: this only really for the sake of politeness, as rtc couple
    // implementation handles the slave attempting to create an offer
    if (signaller.isMaster(data.id)) {
      monitor.createOffer();
    }
  }