diff --git a/.zuul.yml b/.zuul.yml index 6cf6dad7..e38b404b 100644 --- a/.zuul.yml +++ b/.zuul.yml @@ -5,5 +5,4 @@ tunnel: authtoken: 6Aw8vTgcG5EvXdQywVvbh_3fMxvd4Q7dcL2caAHAFjV proto: tcp browserify: - - exclude: bufferutil - - exclude: utf-8-validate + - ignore: ws diff --git a/README.md b/README.md index 2cdcbecb..19d31491 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Engine.IO is a commonjs module, which means you can include it by using 1. build your app bundle ```bash - $ browserify app.js > bundle.js + $ browserify app.js -i ws > bundle.js ``` 1. include on your page diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index f2784658..2f0e45ee 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -8,6 +8,7 @@ var parseqs = require('parseqs'); var inherit = require('component-inherit'); var yeast = require('yeast'); var debug = require('debug')('engine.io-client:websocket'); +var BrowserWebSocket = global.WebSocket || global.MozWebSocket; /** * Get either the `WebSocket` or `MozWebSocket` globals @@ -15,7 +16,7 @@ var debug = require('debug')('engine.io-client:websocket'); * exposed by `ws` for Node environment. */ -var WebSocket = typeof window !== 'undefined' ? (window.WebSocket || window.MozWebSocket) : require('ws'); +var WebSocket = BrowserWebSocket || (typeof window !== 'undefined' ? null : require('ws')); /** * Module exports. @@ -91,7 +92,7 @@ WS.prototype.doOpen = function(){ opts.headers = this.extraHeaders; } - this.ws = new WebSocket(uri, protocols, opts); + this.ws = BrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts); if (this.ws.binaryType === undefined) { this.supportsBinary = false; @@ -158,15 +159,13 @@ WS.prototype.write = function(packets){ var self = this; this.writable = false; - var isBrowserWebSocket = global.WebSocket && this.ws instanceof global.WebSocket; - // encodePacket efficient as it uses WS framing // no need for encodePayload var total = packets.length; for (var i = 0, l = total; i < l; i++) { (function(packet) { parser.encodePacket(packet, self.supportsBinary, function(data) { - if (!isBrowserWebSocket) { + if (!BrowserWebSocket) { // always create a new object (GH-437) var opts = {}; if (packet.options) { @@ -185,7 +184,7 @@ WS.prototype.write = function(packets){ //have a chance of informing us about it yet, in that case send will //throw an error try { - if (isBrowserWebSocket) { + if (BrowserWebSocket) { // TypeError is thrown when passing the second argument on Safari self.ws.send(data); } else { diff --git a/support/browserify.js b/support/browserify.js index ad976a4c..9126a9db 100644 --- a/support/browserify.js +++ b/support/browserify.js @@ -28,8 +28,7 @@ function build(fn){ insertGlobalVars: { global: glob }, standalone: 'eio' }) - .exclude('bufferutil') - .exclude('utf-8-validate') + .ignore('ws') .bundle(); bundle.on('error', function (err) { diff --git a/test/support/env.js b/test/support/env.js index ce1408c8..3551fa45 100644 --- a/test/support/env.js +++ b/test/support/env.js @@ -3,7 +3,7 @@ // support in browsers and in node.js // some tests do not yet work in both exports.browser = !!global.window; -exports.wsSupport = !!require('ws'); +exports.wsSupport = !!(!global.window || window.WebSocket || window.MozWebSocket); var userAgent = global.navigator ? navigator.userAgent : ''; exports.isOldSimulator = ~userAgent.indexOf('iPhone OS 4') || ~userAgent.indexOf('iPhone OS 5');