Merge pull request #447 from darrachequesne/patch-2

Fix ws in browser
This commit is contained in:
Guillermo Rauch
2016-01-03 20:20:18 -03:00
5 changed files with 9 additions and 12 deletions

View File

@@ -5,5 +5,4 @@ tunnel:
authtoken: 6Aw8vTgcG5EvXdQywVvbh_3fMxvd4Q7dcL2caAHAFjV
proto: tcp
browserify:
- exclude: bufferutil
- exclude: utf-8-validate
- ignore: ws

View File

@@ -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

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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');