Possibility to disable JSONP. Emitting an error if JSONP is the only available transport.

This commit is contained in:
Tony Kovanen
2014-06-23 01:31:17 +03:00
parent c090f20211
commit 269bdfff23
3 changed files with 44 additions and 2 deletions

View File

@@ -71,6 +71,7 @@ function Socket(uri, opts){
this.upgrade = false !== opts.upgrade;
this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
this.forceJSONP = !!opts.forceJSONP;
this.noJSONP = !!opts.noJSONP;
this.forceBase64 = !!opts.forceBase64;
this.timestampParam = opts.timestampParam || 't';
this.timestampRequests = opts.timestampRequests;
@@ -140,6 +141,7 @@ Socket.prototype.createTransport = function (name) {
path: this.path,
query: query,
forceJSONP: this.forceJSONP,
noJSONP: this.noJSONP,
forceBase64: this.forceBase64,
timestampRequests: this.timestampRequests,
timestampParam: this.timestampParam,
@@ -169,11 +171,26 @@ Socket.prototype.open = function () {
var transport;
if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) {
transport = 'websocket';
} else if (0 == this.transports.length) {
// Emit error on next tick so it can be listened to
var self = this;
setTimeout(function() {
self.emit('error', 'No transports available');
}, 0);
return;
} else {
transport = this.transports[0];
}
this.readyState = 'opening';
var transport = this.createTransport(transport);
// If polling and JSONP disabled (constructor returns null)
if (!transport.name) {
this.transports.shift();
this.open();
return;
}
transport.open();
this.setTransport(transport);
};

View File

@@ -43,6 +43,6 @@ function polling(opts){
if ('open' in xhr && !opts.forceJSONP) {
return new XHR(opts);
} else {
return new JSONP(opts);
return opts.noJSONP ? null : new JSONP(opts);
}
}