Skip to content

Commit

Permalink
fix(PeerGroup): handle lack of WebRTC support
Browse files Browse the repository at this point in the history
When WebRTC is not supported (old browsers, or in Node.js without the "wrtc" package installed, don't try to get WebTC peers from a web seed. Also, throw an error if we try to listen for web connections.
  • Loading branch information
mappum committed May 29, 2015
1 parent 0f575da commit 42d4077
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/peerGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ var u = require('./utils.js')

var webSeeds = require('./constants.js').webSeeds

var supportsWebRTC = false
if (process.browser) {
var RTCPC = window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.RTCPeerConnection
supportsWebRTC = !!RTCPC
} else {
try {
require('wrtc')
supportsWebRTC = true
} catch(e) {}
}

// HACK: suppress warnings from Buffer#get()
Buffer.prototype.get = function get (offset) {
return this.readUInt8(offset)
Expand Down Expand Up @@ -151,14 +162,16 @@ PeerGroup.prototype.connect = function (opts, cb) {
})
}

webSeeds.forEach(function (uri) {
var client = new PeerhubClient(uri, function () {
self.webSeeds.push(client)
self.emit('seedconnect', client)
self._connectToWebPeers()
if (self.acceptWeb) self._acceptFromPeerhub(client)
if (supportsWebRTC) {
webSeeds.forEach(function (uri) {
var client = new PeerhubClient(uri, function () {
self.webSeeds.push(client)
self.emit('seedconnect', client)
self._connectToWebPeers()
if (self.acceptWeb) self._acceptFromPeerhub(client)
})
})
})
}
}

PeerGroup.prototype._connectToLocalhost = function (cb) {
Expand Down Expand Up @@ -231,6 +244,7 @@ PeerGroup.prototype._onWebPeerConnect = function (conn, incoming) {
}

PeerGroup.prototype.acceptWebPeers = function () {
if (!supportsWebRTC) throw new Error('WebRTC is not supported')
this.webSeeds.forEach(this._acceptFromPeerhub.bind(this))
this.acceptWeb = true
}
Expand Down

0 comments on commit 42d4077

Please sign in to comment.