Skip to content

Commit 8f6e65d

Browse files
committed
Merge pull request #447 from darrachequesne/patch-2
Fix ws in browser
2 parents a851d05 + deb9805 commit 8f6e65d

File tree

5 files changed

+9
-12
lines changed

5 files changed

+9
-12
lines changed

.zuul.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ tunnel:
55
authtoken: 6Aw8vTgcG5EvXdQywVvbh_3fMxvd4Q7dcL2caAHAFjV
66
proto: tcp
77
browserify:
8-
- exclude: bufferutil
9-
- exclude: utf-8-validate
8+
- ignore: ws

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Engine.IO is a commonjs module, which means you can include it by using
5151
1. build your app bundle
5252
5353
```bash
54-
$ browserify app.js > bundle.js
54+
$ browserify app.js -i ws > bundle.js
5555
```
5656
5757
1. include on your page

lib/transports/websocket.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ var parseqs = require('parseqs');
88
var inherit = require('component-inherit');
99
var yeast = require('yeast');
1010
var debug = require('debug')('engine.io-client:websocket');
11+
var BrowserWebSocket = global.WebSocket || global.MozWebSocket;
1112

1213
/**
1314
* Get either the `WebSocket` or `MozWebSocket` globals
1415
* in the browser or the WebSocket-compatible interface
1516
* exposed by `ws` for Node environment.
1617
*/
1718

18-
var WebSocket = typeof window !== 'undefined' ? (window.WebSocket || window.MozWebSocket) : require('ws');
19+
var WebSocket = BrowserWebSocket || (typeof window !== 'undefined' ? null : require('ws'));
1920

2021
/**
2122
* Module exports.
@@ -91,7 +92,7 @@ WS.prototype.doOpen = function(){
9192
opts.headers = this.extraHeaders;
9293
}
9394

94-
this.ws = new WebSocket(uri, protocols, opts);
95+
this.ws = BrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts);
9596

9697
if (this.ws.binaryType === undefined) {
9798
this.supportsBinary = false;
@@ -158,15 +159,13 @@ WS.prototype.write = function(packets){
158159
var self = this;
159160
this.writable = false;
160161

161-
var isBrowserWebSocket = global.WebSocket && this.ws instanceof global.WebSocket;
162-
163162
// encodePacket efficient as it uses WS framing
164163
// no need for encodePayload
165164
var total = packets.length;
166165
for (var i = 0, l = total; i < l; i++) {
167166
(function(packet) {
168167
parser.encodePacket(packet, self.supportsBinary, function(data) {
169-
if (!isBrowserWebSocket) {
168+
if (!BrowserWebSocket) {
170169
// always create a new object (GH-437)
171170
var opts = {};
172171
if (packet.options) {
@@ -185,7 +184,7 @@ WS.prototype.write = function(packets){
185184
//have a chance of informing us about it yet, in that case send will
186185
//throw an error
187186
try {
188-
if (isBrowserWebSocket) {
187+
if (BrowserWebSocket) {
189188
// TypeError is thrown when passing the second argument on Safari
190189
self.ws.send(data);
191190
} else {

support/browserify.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ function build(fn){
2828
insertGlobalVars: { global: glob },
2929
standalone: 'eio'
3030
})
31-
.exclude('bufferutil')
32-
.exclude('utf-8-validate')
31+
.ignore('ws')
3332
.bundle();
3433

3534
bundle.on('error', function (err) {

test/support/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// support in browsers and in node.js
44
// some tests do not yet work in both
55
exports.browser = !!global.window;
6-
exports.wsSupport = !!require('ws');
6+
exports.wsSupport = !!(!global.window || window.WebSocket || window.MozWebSocket);
77

88
var userAgent = global.navigator ? navigator.userAgent : '';
99
exports.isOldSimulator = ~userAgent.indexOf('iPhone OS 4') || ~userAgent.indexOf('iPhone OS 5');

0 commit comments

Comments
 (0)