Skip to content

Commit

Permalink
unmerge firefox for now
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle committed Apr 3, 2013
1 parent b783e39 commit 34e704e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 267 deletions.
150 changes: 21 additions & 129 deletions dist/peer.js
Expand Up @@ -721,7 +721,7 @@ EventEmitter.prototype.emit = function(type) {
var util = { var util = {


chromeCompatible: true, chromeCompatible: true,
firefoxCompatible: true, firefoxCompatible: false,
chromeVersion: 26, chromeVersion: 26,
firefoxVersion: 22, firefoxVersion: 22,


Expand Down Expand Up @@ -1287,12 +1287,6 @@ Peer.prototype._handleServerJSONMessage = function(message) {
var peer = message.src; var peer = message.src;
var manager = this.managers[peer]; var manager = this.managers[peer];
var payload = message.payload; var payload = message.payload;

// Check that browsers match.
if (!!payload && !!payload.browserisms && payload.browserisms !== util.browserisms) {
this._warn('incompatible-peer', 'Peer ' + self.peer + ' is on an incompatible browser. Please clean up this peer.');
}

switch (message.type) { switch (message.type) {
case 'OPEN': case 'OPEN':
this._processQueue(); this._processQueue();
Expand Down Expand Up @@ -1347,11 +1341,10 @@ Peer.prototype._handleServerJSONMessage = function(message) {
this._abort('invalid-key', 'API KEY "' + this._key + '" is invalid'); this._abort('invalid-key', 'API KEY "' + this._key + '" is invalid');
break; break;
case 'PORT': case 'PORT':
// Firefoxism: exchanging ports. //if (util.browserisms === 'Firefox') {
if (util.browserisms === 'Firefox' && manager) { // connection.handlePort(payload);
manager.handlePort(payload); // break;
break; //}
}
default: default:
util.log('Unrecognized message type:', message.type); util.log('Unrecognized message type:', message.type);
break; break;
Expand Down Expand Up @@ -1392,12 +1385,6 @@ Peer.prototype._abort = function(type, message) {
this.destroy(); this.destroy();
this.emit('error', err); this.emit('error', err);
}; };
/** Emits an error message that things may not work. */
Peer.prototype._warn = function(type, message) {
var err = new Error(message);
err.type = type;
this.emit('error', err);
};


Peer.prototype._cleanup = function() { Peer.prototype._cleanup = function() {
var self = this; var self = this;
Expand All @@ -1419,14 +1406,15 @@ Peer.prototype._cleanup = function() {
* is waiting for an ID. */ * is waiting for an ID. */
Peer.prototype.connect = function(peer, options) { Peer.prototype.connect = function(peer, options) {
if (this.disconnected) { if (this.disconnected) {
this._warn('server-disconnected', 'This Peer has been disconnected from the server and can no longer make connections.'); var err = new Error('This Peer has been disconnected from the server and can no longer make connections.');
err.type = 'server-disconnected';
this.emit('error', err);
return; return;
} }


options = util.extend({ options = util.extend({
config: this._options.config config: this._options.config
}, options); }, options);
options.originator = true;


var manager = this.managers[peer]; var manager = this.managers[peer];
if (!manager) { if (!manager) {
Expand Down Expand Up @@ -1491,12 +1479,10 @@ function DataConnection(peer, dc, options) {
this.open = false; this.open = false;


this.label = options.label; this.label = options.label;
// Firefox is not this finely configurable.
this.metadata = options.metadata; this.metadata = options.metadata;
this.serialization = util.browserisms !== 'Firefox' ? options.serialization : 'binary'; this.serialization = options.serialization;
this._isReliable = util.browserisms !== 'Firefox' ? options.reliable : false;

this.peer = peer; this.peer = peer;
this._isReliable = options.reliable;


this._dc = dc; this._dc = dc;
if (!!this._dc) { if (!!this._dc) {
Expand All @@ -1507,7 +1493,6 @@ function DataConnection(peer, dc, options) {
util.inherits(DataConnection, EventEmitter); util.inherits(DataConnection, EventEmitter);


DataConnection.prototype._configureDataChannel = function() { DataConnection.prototype._configureDataChannel = function() {
util.log('Configuring DataChannel with peer ' + this.peer);
var self = this; var self = this;
if (util.browserisms !== 'Webkit') { if (util.browserisms !== 'Webkit') {
this._dc.binaryType = 'arraybuffer'; this._dc.binaryType = 'arraybuffer';
Expand Down Expand Up @@ -1666,80 +1651,25 @@ ConnectionManager.prototype.initialize = function(id, socket) {
this._socket = socket; this._socket = socket;
} }


// Firefoxism where ports need to be generated.
if (util.browserisms === 'Firefox') {
this._firefoxPortSetup();
}

// Set up PeerConnection. // Set up PeerConnection.
this._startPeerConnection(); this._startPeerConnection();


// Process queued DCs. // Process queued DCs.
if (util.browserisms !== 'Firefox') { this._processQueue();
this._processQueue();
}


// Listen for ICE candidates. // Listen for ICE candidates.
this._setupIce(); this._setupIce();


// Listen for negotiation needed.
// Chrome only **
this._setupNegotiationHandler();

// Listen for data channel. // Listen for data channel.
this._setupDataChannel(); this._setupDataChannel();


// Listen for negotiation needed.
// Chrome only--Firefox instead has to manually makeOffer.
if (util.browserisms !== 'Firefox') {
this._setupNegotiationHandler();
} else if (this._options.originator) {
this._firefoxHandlerSetup()
this._firefoxAdditional()
}

this.initialize = function() { }; this.initialize = function() { };
}; };


/** Firefoxism that requires a fake media stream. */
ConnectionManager.prototype._firefoxAdditional = function() {
util.log('Additional media stream for Firefox.');
var self = this;
getUserMedia({ audio: true, fake: true }, function(s) {
self.pc.addStream(s);
if (self._options.originator) {
self._makeOffer();
} else {
self._makeAnswer();
}
}, function(err) { util.log('Could not getUserMedia'); });
};

/** Firefoxism that requires ports to be set up. */
ConnectionManager.prototype._firefoxPortSetup = function() {
if (!ConnectionManager.usedPorts) {
ConnectionManager.usedPorts = [];
}
this._localPort = util.randomPort();
while (ConnectionManager.usedPorts.indexOf(this._localPort) != -1) {
this._localPort = util.randomPort();
}
this._remotePort = util.randomPort();
while (this._remotePort === this._localPort ||
ConnectionManager.usedPorts.indexOf(this._remotePort) != -1) {
this._remotePort = util.randomPort();
}
ConnectionManager.usedPorts.push(this._remotePort);
ConnectionManager.usedPorts.push(this._localPort);
};

/** Firefoxism that is `onconnection`. */
ConnectionManager.prototype._firefoxHandlerSetup = function() {
util.log('Setup Firefox `onconnection`.');
var self = this;
this.pc.onconnection = function() {
util.log('FIREFOX: onconnection triggered');

self._processQueue();
}
};

/** Start a PC. */ /** Start a PC. */
ConnectionManager.prototype._startPeerConnection = function() { ConnectionManager.prototype._startPeerConnection = function() {
util.log('Creating RTCPeerConnection'); util.log('Creating RTCPeerConnection');
Expand Down Expand Up @@ -1788,17 +1718,11 @@ ConnectionManager.prototype._setupDataChannel = function() {
util.log('Listening for data channel'); util.log('Listening for data channel');
this.pc.ondatachannel = function(evt) { this.pc.ondatachannel = function(evt) {
util.log('Received data channel'); util.log('Received data channel');
// Firefoxism: ondatachannel receives channel directly. NOT TO SPEC. var dc = evt.channel;
var dc = util.browserisms === 'Firefox' ? evt : evt.channel;
var label = dc.label; var label = dc.label;

// This should not be empty. // This should not be empty.
// NOTE: Multiple DCs are currently not configurable in FF. Will have to var options = self.labels[label] || {};
// come up with reasonable defaults.
var options = self.labels[label] || { label: label };
var connection = new DataConnection(self.peer, dc, options); var connection = new DataConnection(self.peer, dc, options);
delete self.labels[label];

self._attachConnectionListeners(connection); self._attachConnectionListeners(connection);
self.connections[label] = connection; self.connections[label] = connection;
self.emit('connection', connection); self.emit('connection', connection);
Expand All @@ -1815,7 +1739,6 @@ ConnectionManager.prototype._makeOffer = function() {
self._socket.send({ self._socket.send({
type: 'OFFER', type: 'OFFER',
payload: { payload: {
browserisms: util.browserisms,
sdp: offer, sdp: offer,
config: self._options.config, config: self._options.config,
labels: self.labels labels: self.labels
Expand All @@ -1841,7 +1764,6 @@ ConnectionManager.prototype._makeAnswer = function() {
self._socket.send({ self._socket.send({
type: 'ANSWER', type: 'ANSWER',
payload: { payload: {
browserisms: util.browserisms,
sdp: answer sdp: answer
}, },
dst: self.peer dst: self.peer
Expand Down Expand Up @@ -1892,45 +1814,15 @@ ConnectionManager.prototype._attachConnectionListeners = function(connection) {
}); });
}; };


/** Firefoxism: handle receiving a set of ports. */
ConnectionManager.prototype.handlePort = function(ports) {
util.log('Received ports, calling connectDataConnection.');
if (!ConnectionManager.usedPorts) {
ConnectionManager.usedPorts = [];
}
ConnectionManager.usedPorts.push(ports.local);
ConnectionManager.usedPorts.push(ports.remote);
this.pc.connectDataConnection(ports.local, ports.remote);
};

/** Handle an SDP. */ /** Handle an SDP. */
ConnectionManager.prototype.handleSDP = function(sdp, type) { ConnectionManager.prototype.handleSDP = function(sdp, type) {
if (util.browserisms !== 'Firefox') { sdp = new RTCSessionDescription(sdp);
// Doesn't need to happen for FF.
sdp = new RTCSessionDescription(sdp);
}


var self = this; var self = this;
this.pc.setRemoteDescription(sdp, function() { this.pc.setRemoteDescription(sdp, function() {
util.log('Set remoteDescription: ' + type); util.log('Set remoteDescription: ' + type);
if (type === 'OFFER') { if (type === 'OFFER') {
if (util.browserisms === 'Firefox') { self._makeAnswer();
self._firefoxAdditional();
} else {
self._makeAnswer();
}
} else if (util.browserisms === 'Firefox') {
// Firefoxism.
util.log('Peer ANSWER received, connectDataConnection called.');
self.pc.connectDataConnection(self._localPort, self._remotePort);
self._socket.send({
type: 'PORT',
payload: {
remote: self._localPort,
local: self._remotePort
},
dst: self.peer
});
} }
}, function(err) { }, function(err) {
self.emit('error', err); self.emit('error', err);
Expand Down Expand Up @@ -1987,14 +1879,14 @@ ConnectionManager.prototype.connect = function(options) {
this.labels[options.label] = options; this.labels[options.label] = options;


var dc; var dc;
if (!!this.pc && !this._lock && (util.browserisms !== 'Firefox' || Object.keys(this.connections).length !== 0)) { if (!!this.pc && !this._lock) {
dc = this.pc.createDataChannel(options.label, { reliable: false }); dc = this.pc.createDataChannel(options.label, { reliable: false });
} }
var connection = new DataConnection(this.peer, dc, options); var connection = new DataConnection(this.peer, dc, options);
this._attachConnectionListeners(connection); this._attachConnectionListeners(connection);
this.connections[options.label] = connection; this.connections[options.label] = connection;


if (!dc) { if (!this.pc || this._lock) {
this._queued.push(connection); this._queued.push(connection);
} }


Expand Down
2 changes: 1 addition & 1 deletion dist/peer.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/api.md
Expand Up @@ -46,9 +46,9 @@ Returns a `DataConnection` object.
* `id` String. The id of the remote peer to connect to. * `id` String. The id of the remote peer to connect to.
* `options` Object. * `options` Object.
* `label` Optional label for the underlying DataChannel, to differentiate between DataConnections between the same two peers. If left unspecified, a label will be assigned at random. * `label` Optional label for the underlying DataChannel, to differentiate between DataConnections between the same two peers. If left unspecified, a label will be assigned at random.
* `metadata` Optional metadata to pass to the remote peer. Can be any serializable type. **Only available in Firefox on the first DataConnection established between two Peers.** * `metadata` Optional metadata to pass to the remote peer. Can be any serializable type.
* `serialization` String, which can be `binary`, `binary-utf8`, `json`, or `none`. This will be the serialization format of all data sent over the P2P DataConnection. Defaults to `binary`. **Will always be `binary` in Firefox.** * `serialization` String, which can be `binary`, `binary-utf8`, `json`, or `none`. This will be the serialization format of all data sent over the P2P DataConnection. Defaults to `binary`.
* `reliable` Boolean, which if `true` activates experimental reliable transfer (while waiting for actual reliable transfer to be implemented in Chrome). Defaults to `false` until Chrome implements reliable/large data transfer. This parameter is only available in the most recent build. **Will always be `true` in Firefox.** * `reliable` Boolean, which if `true` activates experimental reliable transfer (while waiting for actual reliable transfer to be implemented in Chrome). Defaults to `false` until Chrome implements reliable/large data transfer. This parameter is only available in the most recent build.


Before writing to / data will be emitted from the `DataConnection` object that is returned, the `open` event must fire. Also the `error` event should be checked in case a connection cannot be made. Before writing to / data will be emitted from the `DataConnection` object that is returned, the `open` event must fire. Also the `error` event should be checked in case a connection cannot be made.


Expand Down Expand Up @@ -86,7 +86,8 @@ This event does not need to fire before creating or receiving connections.


`function (error) { }` `function (error) { }`


Emitted when an unexpected event occurs. All errors on the Peer except `server-disconnected` and `incompatible-peer` are **fatal**. Errors from the underlying socket and PeerConnections are forwarded here. Emitted when an unexpected event occurs. Errors on the Peer are **always
fatal**. Errors from the underlying socket and PeerConnections are forwarded here.


The `error` object also has a `type` parameter that may be helpful in responding to client errors properly: The `error` object also has a `type` parameter that may be helpful in responding to client errors properly:
* `browser-incompatible`: The client's browser does not support some or all WebRTC features that you are trying to use. * `browser-incompatible`: The client's browser does not support some or all WebRTC features that you are trying to use.
Expand All @@ -99,7 +100,6 @@ The `error` object also has a `type` parameter that may be helpful in responding
* `socket-closed`: The underlying socket closed unexpectedly. * `socket-closed`: The underlying socket closed unexpectedly.
* (The Peer object is destroyed after one of the errors above are emitted.) * (The Peer object is destroyed after one of the errors above are emitted.)
* `server-disconnected`: A Peer that has been disconnected is being used to try to connect. * `server-disconnected`: A Peer that has been disconnected is being used to try to connect.
* `incompatible-peer`: The peer that is trying to connect or that you are trying to connect to is using a browser that is different from yours. **Currently DataChannels are not interoperable between Chrome and Firefox.**


### Event: 'close' ### Event: 'close'


Expand Down
3 changes: 1 addition & 2 deletions examples/chat.html
Expand Up @@ -9,10 +9,9 @@


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script> <script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script>
<script type="text/javascript" src="../dist/peer.js"></script>
<script> <script>
// Connect to PeerJS, have server assign an ID instead of providing one // Connect to PeerJS, have server assign an ID instead of providing one
peer = new Peer({key: 'lwjd5qra8257b9', debug: true}); var peer = new Peer({key: 'lwjd5qra8257b9', debug: true});


// Show this peer's ID. // Show this peer's ID.
peer.on('open', function(id){ peer.on('open', function(id){
Expand Down
2 changes: 1 addition & 1 deletion examples/helloworld.html
Expand Up @@ -7,12 +7,12 @@


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script> <script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script>
<script src="../dist/peer.js"></script>
<script> <script>
// This is a very simple code example. See chat.html for a more involved // This is a very simple code example. See chat.html for a more involved
// example. // example.


$(document).ready(function() { $(document).ready(function() {
var peer1, peer2, peerId1;


// Create a new Peer with our demo API key, with debug set to true so we can // Create a new Peer with our demo API key, with debug set to true so we can
// see what's going on. // see what's going on.
Expand Down

0 comments on commit 34e704e

Please sign in to comment.