diff --git a/client.js b/client.js index d9aa6b4..3b52376 100644 --- a/client.js +++ b/client.js @@ -87,24 +87,25 @@ function Client (peerId, port, torrent, opts) { } else if (protocol === 'udp:' && typeof UDPTracker === 'function') { return new UDPTracker(self, announceUrl) } else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) { - // Don't try to add http tracker on an https website - if (protocol === 'ws:' && location && location.protocol && location.protocol === 'https:') { - process.nextTick(function () { - var err = new Error('unsupported http tracker on https: ' + announceUrl) - self.emit('warning', err) - }) + // Skip ws:// trackers on https:// sites because they throw SecurityError + if (protocol === 'ws:' && typeof window !== 'undefined' && + window.location.protocol === 'https:') { + nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl)) return null } return new WebSocketTracker(self, announceUrl) } else { - process.nextTick(function () { - var err = new Error('unsupported tracker protocol for ' + announceUrl) - self.emit('warning', err) - }) + nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl)) + return null } - return null }) .filter(Boolean) + + function nextTickWarn (err) { + process.nextTick(function () { + self.emit('warning', err) + }) + } } /**