engine.io-client
Advanced tools
Comparing version 0.9.0 to 1.0.0
1.0.0 / 2014-03-06 | ||
================== | ||
* run browserify without shims | ||
* emit socket upgrade event after upgrade done | ||
* better feature detection for XHR2 | ||
* added `rememberUpgrade` option | ||
* binary support | ||
0.9.0 / 2014-02-09 | ||
@@ -3,0 +12,0 @@ ================== |
@@ -78,2 +78,3 @@ /** | ||
this.forceJSONP = !!opts.forceJSONP; | ||
this.forceBase64 = !!opts.forceBase64; | ||
this.timestampParam = opts.timestampParam || 't'; | ||
@@ -87,5 +88,10 @@ this.timestampRequests = opts.timestampRequests; | ||
this.policyPort = opts.policyPort || 843; | ||
this.rememberUpgrade = opts.rememberUpgrade || false; | ||
this.open(); | ||
this.binaryType = null; | ||
this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades; | ||
} | ||
Socket.priorWebsocketSuccess = false; | ||
/** | ||
@@ -146,6 +152,8 @@ * Mix in `Emitter`. | ||
forceJSONP: this.forceJSONP, | ||
forceBase64: this.forceBase64, | ||
timestampRequests: this.timestampRequests, | ||
timestampParam: this.timestampParam, | ||
flashPath: this.flashPath, | ||
policyPort: this.policyPort | ||
policyPort: this.policyPort, | ||
socket: this | ||
}); | ||
@@ -171,5 +179,9 @@ | ||
*/ | ||
Socket.prototype.open = function () { | ||
var transport = this.transports[0]; | ||
var transport; | ||
if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) { | ||
transport = 'websocket'; | ||
} else { | ||
transport = this.transports[0]; | ||
} | ||
this.readyState = 'opening'; | ||
@@ -228,3 +240,9 @@ var transport = this.createTransport(transport); | ||
Socket.priorWebsocketSuccess = false; | ||
transport.once('open', function () { | ||
if (this.onlyBinaryUpgrades) { | ||
var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary; | ||
failed = failed || upgradeLosesBinary; | ||
} | ||
if (failed) return; | ||
@@ -240,2 +258,3 @@ | ||
self.emit('upgrading', transport); | ||
Socket.priorWebsocketSuccess = 'websocket' == transport.name; | ||
@@ -250,5 +269,5 @@ debug('pausing current transport "%s"', self.transport.name); | ||
transport.removeListener('error', onerror); | ||
self.emit('upgrade', transport); | ||
self.setTransport(transport); | ||
transport.send([{ type: 'upgrade' }]); | ||
self.emit('upgrade', transport); | ||
transport = null; | ||
@@ -314,2 +333,3 @@ self.upgrading = false; | ||
this.readyState = 'open'; | ||
Socket.priorWebsocketSuccess = 'websocket' == this.transport.name; | ||
this.emit('open'); | ||
@@ -366,2 +386,3 @@ this.onopen && this.onopen.call(this); | ||
}; | ||
this.onmessage && this.onmessage.call(this, event); | ||
@@ -445,3 +466,3 @@ break; | ||
Socket.prototype.onDrain = function() { | ||
Socket.prototype.onDrain = function() { | ||
for (var i = 0; i < this.prevBufferLen; i++) { | ||
@@ -542,2 +563,3 @@ if (this.callbackBuffer[i]) { | ||
debug('socket error %j', err); | ||
Socket.priorWebsocketSuccess = false; | ||
this.emit('error', err); | ||
@@ -544,0 +566,0 @@ this.onerror && this.onerror.call(this, err); |
@@ -1,2 +0,1 @@ | ||
/** | ||
@@ -33,2 +32,3 @@ * Module dependencies. | ||
this.agent = opts.agent || false; | ||
this.socket = opts.socket; | ||
} | ||
@@ -123,3 +123,3 @@ | ||
Transport.prototype.onData = function (data) { | ||
this.onPacket(parser.decodePacket(data)); | ||
this.onPacket(parser.decodePacket(data, this.socket.binaryType)); | ||
}; | ||
@@ -126,0 +126,0 @@ |
@@ -54,2 +54,8 @@ | ||
/* | ||
* FlashSockets only support binary as base64 encoded strings | ||
*/ | ||
FlashWS.prototype.supportsBinary = false; | ||
/** | ||
@@ -95,3 +101,3 @@ * Opens the transport. | ||
WebSocket.__addTask(function () { | ||
self.socket = new WebSocket(self.uri()); | ||
self.ws = new WebSocket(self.uri()); | ||
self.addEventListeners(); | ||
@@ -110,3 +116,3 @@ }); | ||
FlashWS.prototype.doClose = function(){ | ||
if (!this.socket) return; | ||
if (!this.ws) return; | ||
var self = this; | ||
@@ -113,0 +119,0 @@ WebSocket.__addTask(function(){ |
@@ -82,2 +82,8 @@ | ||
/* | ||
* JSONP only supports binary as base64 encoded strings | ||
*/ | ||
JSONPPolling.prototype.supportsBinary = false; | ||
/** | ||
@@ -84,0 +90,0 @@ * Closes the socket |
@@ -67,2 +67,8 @@ /** | ||
/** | ||
* XHR supports binary | ||
*/ | ||
XHR.prototype.supportsBinary = true; | ||
/** | ||
* Creates a request. | ||
@@ -79,2 +85,3 @@ * | ||
opts.agent = this.agent || false; | ||
opts.supportsBinary = this.supportsBinary; | ||
return new Request(opts); | ||
@@ -92,3 +99,4 @@ }; | ||
XHR.prototype.doWrite = function(data, fn){ | ||
var req = this.request({ method: 'POST', data: data }); | ||
var isBinary = typeof data !== 'string' && data !== undefined; | ||
var req = this.request({ method: 'POST', data: data, isBinary: isBinary }); | ||
var self = this; | ||
@@ -135,3 +143,3 @@ req.on('success', fn); | ||
this.agent = opts.agent; | ||
this.create(); | ||
this.create(opts.isBinary, opts.supportsBinary); | ||
} | ||
@@ -151,3 +159,3 @@ | ||
Request.prototype.create = function(){ | ||
Request.prototype.create = function(isBinary, supportsBinary){ | ||
var xhr = this.xhr = new XMLHttpRequest({ agent: this.agent, xdomain: this.xd }); | ||
@@ -159,6 +167,15 @@ var self = this; | ||
xhr.open(this.method, this.uri, this.async); | ||
if (supportsBinary) { | ||
// This has to be done after open because Firefox is stupid | ||
// http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension | ||
xhr.responseType = 'arraybuffer'; | ||
} | ||
if ('POST' == this.method) { | ||
try { | ||
xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8'); | ||
if (isBinary) { | ||
xhr.setRequestHeader('Content-type', 'application/octet-stream'); | ||
} else { | ||
xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8'); | ||
} | ||
} catch (e) {} | ||
@@ -178,3 +195,12 @@ } | ||
if (200 == xhr.status || 1223 == xhr.status) { | ||
data = xhr.responseText; | ||
var contentType = xhr.getResponseHeader('Content-Type'); | ||
if (contentType === 'application/octet-stream') { | ||
data = xhr.response; | ||
} else { | ||
if (!supportsBinary) { | ||
data = xhr.responseText; | ||
} else { | ||
data = 'ok'; | ||
} | ||
} | ||
} else { | ||
@@ -199,3 +225,3 @@ // make sure the `error` event handler that's user-set | ||
} catch (e) { | ||
// Need to defer since .create() is called directly from the constructor | ||
// Need to defer since .create() is called directly fhrom the constructor | ||
// and thus the 'error' event can only be only bound *after* this exception | ||
@@ -291,3 +317,3 @@ // occurs. Therefore, also, we cannot throw here at all. | ||
global.attachEvent('onunload', function(){ | ||
function unloadHandler() { | ||
for (var i in Request.requests) { | ||
@@ -298,3 +324,5 @@ if (Request.requests.hasOwnProperty(i)) { | ||
} | ||
}); | ||
} | ||
global.attachEvent('onunload', unloadHandler); | ||
} |
@@ -23,2 +23,12 @@ /** | ||
/** | ||
* Is XHR2 supported? | ||
*/ | ||
var hasXHR2 = (function() { | ||
var XMLHttpRequest = require('xmlhttprequest'); | ||
var xhr = new XMLHttpRequest({ agent: this.agent, xdomain: false }); | ||
return null != xhr.responseType; | ||
})(); | ||
/** | ||
* Polling interface. | ||
@@ -31,2 +41,6 @@ * | ||
function Polling(opts){ | ||
var forceBase64 = (opts && opts.forceBase64); | ||
if (!hasXHR2 || forceBase64) { | ||
this.supportsBinary = false; | ||
} | ||
Transport.call(this, opts); | ||
@@ -124,5 +138,3 @@ } | ||
debug('polling got data %s', data); | ||
// decode payload | ||
parser.decodePayload(data, function(packet, index, total) { | ||
var callback = function(packet, index, total) { | ||
// if its the first message we consider the transport open | ||
@@ -141,4 +153,7 @@ if ('opening' == self.readyState) { | ||
self.onPacket(packet); | ||
}); | ||
}; | ||
// decode payload | ||
parser.decodePayload(data, this.socket.binaryType, callback); | ||
// if an event did not trigger closing | ||
@@ -194,5 +209,10 @@ if ('closed' != this.readyState) { | ||
this.writable = false; | ||
this.doWrite(parser.encodePayload(packets), function(){ | ||
var callbackfn = function() { | ||
self.writable = true; | ||
self.emit('drain'); | ||
}; | ||
var self = this; | ||
parser.encodePayload(packets, this.supportsBinary, function(data) { | ||
self.doWrite(data, callbackfn); | ||
}); | ||
@@ -223,2 +243,6 @@ }; | ||
if (!this.supportsBinary && !query.sid) { | ||
query.b64 = 1; | ||
} | ||
query = util.qs(query); | ||
@@ -225,0 +249,0 @@ |
@@ -38,2 +38,6 @@ /** | ||
function WS(opts){ | ||
var forceBase64 = (opts && opts.forceBase64); | ||
if (forceBase64) { | ||
this.supportsBinary = false; | ||
} | ||
Transport.call(this, opts); | ||
@@ -56,2 +60,8 @@ } | ||
/* | ||
* WebSockets support binary | ||
*/ | ||
WS.prototype.supportsBinary = true; | ||
/** | ||
@@ -74,3 +84,9 @@ * Opens socket. | ||
this.socket = new WebSocket(uri, protocols, opts); | ||
this.ws = new WebSocket(uri, protocols, opts); | ||
if (this.ws.binaryType !== undefined) { | ||
this.supportsBinary = false; | ||
} | ||
this.ws.binaryType = 'arraybuffer'; | ||
this.addEventListeners(); | ||
@@ -88,12 +104,12 @@ }; | ||
this.socket.onopen = function(){ | ||
this.ws.onopen = function(){ | ||
self.onOpen(); | ||
}; | ||
this.socket.onclose = function(){ | ||
this.ws.onclose = function(){ | ||
self.onClose(); | ||
}; | ||
this.socket.onmessage = function(ev){ | ||
this.ws.onmessage = function(ev){ | ||
self.onData(ev.data); | ||
}; | ||
this.socket.onerror = function(e){ | ||
this.ws.onerror = function(e){ | ||
self.onError('websocket error', e); | ||
@@ -133,4 +149,7 @@ }; | ||
for (var i = 0, l = packets.length; i < l; i++) { | ||
this.socket.send(parser.encodePacket(packets[i])); | ||
parser.encodePacket(packets[i], this.supportsBinary, function(data) { | ||
self.ws.send(data); | ||
}); | ||
} | ||
function ondrain() { | ||
@@ -162,4 +181,4 @@ self.writable = true; | ||
WS.prototype.doClose = function(){ | ||
if (typeof this.socket !== 'undefined') { | ||
this.socket.close(); | ||
if (typeof this.ws !== 'undefined') { | ||
this.ws.close(); | ||
} | ||
@@ -190,2 +209,7 @@ }; | ||
// communicate binary support capabilities | ||
if (!this.supportsBinary) { | ||
query.b64 = 1; | ||
} | ||
query = util.qs(query); | ||
@@ -192,0 +216,0 @@ |
{ | ||
"name": "engine.io-client", | ||
"description": "Client for the realtime Engine", | ||
"version": "0.9.0", | ||
"version": "1.0.0", | ||
"homepage": "http://socket.io", | ||
@@ -31,12 +31,12 @@ "contributors": [ | ||
"indexof": "0.0.1", | ||
"engine.io-parser": "0.3.0", | ||
"engine.io-parser": "1.0.0", | ||
"debug": "0.7.4" | ||
}, | ||
"devDependencies": { | ||
"zuul": "1.5.2", | ||
"zuul": "1.5.4", | ||
"mocha": "1.16.2", | ||
"expect.js": "0.2.0", | ||
"istanbul": "0.2.3", | ||
"browserify": "2.35.1", | ||
"engine.io": "0.8.2", | ||
"browserify": "3.30.1", | ||
"engine.io": "1.0.0", | ||
"express": "3.4.8" | ||
@@ -43,0 +43,0 @@ }, |
@@ -13,2 +13,19 @@ | ||
### Standalone | ||
You can find an `engine.io.js` file in this repository, which is a | ||
standalone build you can use as follows: | ||
```html | ||
<script src="/path/to/engine.io.js"></script> | ||
<script> | ||
// eio = Socket | ||
var socket = eio('ws://localhost'); | ||
socket.onopen = function(){ | ||
socket.onmessage = function(data){}; | ||
socket.onclose = function(){}; | ||
}; | ||
</script> | ||
``` | ||
### With browserify | ||
@@ -43,16 +60,16 @@ | ||
### Standalone | ||
### Sending and receiving binary | ||
If you decide not to use browserify (or similar tool) you can find an `engine.io.js` file in | ||
this repository, which is a standalone build you can use as follows: | ||
```html | ||
<script src="/path/to/engine.io.js"></script> | ||
<script> | ||
// eio = Socket | ||
var socket = eio('ws://localhost'); | ||
socket.onopen = function(){ | ||
socket.onmessage = function(data){}; | ||
socket.onclose = function(){}; | ||
}; | ||
var socket = new eio.Socket('ws://localhost/'); | ||
socket.binaryType = 'blob'; // receives Blob instead of ArrayBuffer (default) | ||
socket.on('open', function () { | ||
socket.send(new Int8Array(5)); | ||
socket.on('message', function (data) { | ||
// data instanceof Blob => true when receiving binary | ||
}); | ||
socket.on('close', function () { }); | ||
}); | ||
</script> | ||
@@ -83,2 +100,10 @@ ``` | ||
- Runs inside HTML5 WebWorker | ||
- Can send and receive binary data | ||
- Receives as ArrayBuffer or Blob when in browser, and Buffer or ArrayBuffer | ||
in Node | ||
- When XHR2 or WebSockets are used, binary is emitted directly. Otherwise | ||
binary is encoded into base64 strings, and decoded when binary types are | ||
supported. | ||
- With browsers that don't support ArrayBuffer, an object { base64: true, | ||
data: dataAsBase64String } is emitted in onmessage | ||
@@ -101,2 +126,5 @@ ## API | ||
- `message` event handler | ||
- `binaryType` _(String)_ : can be set to 'arraybuffer' or 'blob' in browsers, | ||
and `buffer` or `arraybuffer` in Node. Blob is only used in browser if it's | ||
supported. | ||
@@ -110,3 +138,4 @@ #### Events | ||
- **Arguments** | ||
- `String`: utf-8 encoded data | ||
- `String` | `ArrayBuffer`: utf-8 encoded data or ArrayBuffer containing | ||
binary data | ||
- `close` | ||
@@ -123,2 +152,4 @@ - Fired upon disconnection. In compliance with the WebSocket API spec, this event may be | ||
- Fired if an error occurs with a transport we're trying to upgrade to. | ||
- `upgrade` | ||
- Fired upon upgrade success, after the new transport is set | ||
@@ -137,2 +168,3 @@ #### Methods | ||
- `forceJSONP` (`Boolean`): forces JSONP for polling transport. | ||
- `forceBase64` (`Boolean`): forces base 64 encoding for polling transport even when XHR2 responseType is available and WebSocket even if the used standard supports binary. | ||
- `timestampRequests` (`Boolean`): whether to add the timestamp with | ||
@@ -149,6 +181,12 @@ each transport request. Note: this is ignored if the browser is | ||
feature detection test for it passes. | ||
- `rememberUpgrade` (`Boolean`): defaults to false. | ||
If true and if the previous websocket connection to the server succeeded, | ||
the connection attempt will bypass the normal upgrade process and will initially | ||
try websocket. A connection attempt following a transport error will use the | ||
normal upgrade process. It is recommended you turn this on only when using | ||
SSL/TLS connections, or if you know that your network does not block websockets. | ||
- `send` | ||
- Sends a message to the server | ||
- **Parameters** | ||
- `String`: data to send | ||
- `String` | `ArrayBuffer` | `ArrayBufferView` | `Blob`: data to send | ||
- `Function`: optional, callback upon `drain` | ||
@@ -223,3 +261,3 @@ - `close` | ||
Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com> | ||
Copyright (c) 2014 Guillermo Rauch <guillermo@learnboost.com> | ||
@@ -226,0 +264,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
145753
4870
0
273
+ Addedafter@0.8.1(transitive)
+ Addedarraybuffer.slice@0.0.5(transitive)
+ Addedbase64-arraybuffer@0.1.0(transitive)
+ Addedengine.io-parser@1.0.0(transitive)
- Removedengine.io-parser@0.3.0(transitive)
Updatedengine.io-parser@1.0.0