Skip to content

Commit

Permalink
fixes #27, at last. api updates as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle committed Mar 28, 2013
1 parent ed8f854 commit a52201b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
31 changes: 31 additions & 0 deletions dist/peer.js
Expand Up @@ -720,6 +720,11 @@ EventEmitter.prototype.emit = function(type) {

var util = {

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

debug: false,
browserisms: '',

Expand Down Expand Up @@ -816,6 +821,24 @@ var util = {
},
randomToken: function () {
return Math.random().toString(36).substr(2);
},
isBrowserCompatible: function() {
var c, f;
if (this.chromeCompatible) {
if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
// Get version #.
var v = c[1].split('.')[0];
return parseInt(v) >= this.chromeVersion;
}
}
if (this.firefoxCompatible) {
if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
// Get version #.
var v = f[1].split('.')[0];
return parseInt(v) >= this.firefoxVersion;
}
}
return false;
}
};
/**
Expand Down Expand Up @@ -1158,6 +1181,14 @@ function Peer(id, options) {
if (!(this instanceof Peer)) return new Peer(id, options);
EventEmitter.call(this);

// First check if browser can use PeerConnection/DataChannels.
// TODO: when media is supported, lower browser version limit and move DC
// check to where`connect` is called.
if (!util.isBrowserCompatible()) {
this._abort('browser-incompatible', 'The current browser does not support WebRTC DataChannels');
return;
}

// Detect relative URL host.
if (options.host === '/') {
options.host = window.location.hostname;
Expand Down
2 changes: 1 addition & 1 deletion dist/peer.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/api.md
Expand Up @@ -82,6 +82,7 @@ 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:
* `browser-incompatible`: The client's browser does not support some or all WebRTC features that you are trying to use.
* `invalid-id`: The ID passed into the Peer constructor contains illegal characters.
* `invalid-key`: The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).
* `unavailable-id`: The ID passed into the Peer constructor is already taken.
Expand Down
8 changes: 8 additions & 0 deletions lib/peer.js
Expand Up @@ -9,6 +9,14 @@ function Peer(id, options) {
if (!(this instanceof Peer)) return new Peer(id, options);
EventEmitter.call(this);

// First check if browser can use PeerConnection/DataChannels.
// TODO: when media is supported, lower browser version limit and move DC
// check to where`connect` is called.
if (!util.isBrowserCompatible()) {
this._abort('browser-incompatible', 'The current browser does not support WebRTC DataChannels');
return;
}

// Detect relative URL host.
if (options.host === '/') {
options.host = window.location.hostname;
Expand Down
23 changes: 23 additions & 0 deletions lib/util.js
@@ -1,5 +1,10 @@
var util = {

chromeCompatible: true,
firefoxCompatibile: false,

This comment has been minimized.

Copy link
@MrRio

MrRio Mar 28, 2013

This a typo? firefoxCompatible

This comment has been minimized.

Copy link
@michelle

michelle Mar 29, 2013

Author Member

Thanks :)

chromeVersion: 26,
firefoxVersion: 22,

debug: false,
browserisms: '',

Expand Down Expand Up @@ -96,5 +101,23 @@ var util = {
},
randomToken: function () {
return Math.random().toString(36).substr(2);
},
isBrowserCompatible: function() {
var c, f;
if (this.chromeCompatible) {
if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
// Get version #.
var v = c[1].split('.')[0];
return parseInt(v) >= this.chromeVersion;
}
}
if (this.firefoxCompatible) {
if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
// Get version #.
var v = f[1].split('.')[0];
return parseInt(v) >= this.firefoxVersion;
}
}
return false;
}
};

0 comments on commit a52201b

Please sign in to comment.