Skip to content

Commit

Permalink
Added ajax defaults that are used to override any default ajax setups…
Browse files Browse the repository at this point in the history
… to prevent our ajax communications from failing.

#2511
  • Loading branch information
NTaylorMullen committed Sep 20, 2013
1 parent 7f97baf commit e3ffaf1
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 209 deletions.
151 changes: 79 additions & 72 deletions src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js
Expand Up @@ -39,7 +39,13 @@
onStateChanged: "onStateChanged",
onDisconnect: "onDisconnect"
},

ajaxDefaults = {
processData: true,
timeout: null,
async: true,
global: false,
cache: false
},
log = function (msg, logging) {
if (logging === false) {
return;
Expand Down Expand Up @@ -150,6 +156,8 @@

signalR.events = events;

signalR.ajaxDefaults = ajaxDefaults;

signalR.changeState = changeState;

signalR.isDisconnecting = isDisconnecting;
Expand Down Expand Up @@ -457,89 +465,88 @@
url = signalR.transports._logic.addQs(url, connection);

connection.log("Negotiating with '" + url + "'.");
$.ajax({
xhrFields: { withCredentials: connection.withCredentials },
url: url,
global: false,
cache: false,
type: "GET",
contentType: connection.contentType,
data: {},
dataType: connection.ajaxDataType,
error: function (error) {
$(connection).triggerHandler(events.onError, [error.responseText]);
deferred.reject("SignalR: Error during negotiation request: " + error.responseText);
// Stop the connection if negotiate failed
connection.stop();
},
success: function (res) {
var keepAliveData = connection.keepAliveData;

connection.appRelativeUrl = res.Url;
connection.id = res.ConnectionId;
connection.token = res.ConnectionToken;
connection.webSocketServerUrl = res.WebSocketServerUrl;

// Once the server has labeled the PersistentConnection as Disconnected, we should stop attempting to reconnect
// after res.DisconnectTimeout seconds.
connection.disconnectTimeout = res.DisconnectTimeout * 1000; // in ms
$.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
xhrFields: { withCredentials: connection.withCredentials },
url: url,
type: "GET",
contentType: connection.contentType,
data: {},
dataType: connection.ajaxDataType,
error: function (error) {
$(connection).triggerHandler(events.onError, [error.responseText]);
deferred.reject("SignalR: Error during negotiation request: " + error.responseText);
// Stop the connection if negotiate failed
connection.stop();
},
success: function (res) {
var keepAliveData = connection.keepAliveData;

connection.appRelativeUrl = res.Url;
connection.id = res.ConnectionId;
connection.token = res.ConnectionToken;
connection.webSocketServerUrl = res.WebSocketServerUrl;

// Once the server has labeled the PersistentConnection as Disconnected, we should stop attempting to reconnect
// after res.DisconnectTimeout seconds.
connection.disconnectTimeout = res.DisconnectTimeout * 1000; // in ms


// If we have a keep alive
if (res.KeepAliveTimeout) {
// Register the keep alive data as activated
keepAliveData.activated = true;
// If we have a keep alive
if (res.KeepAliveTimeout) {
// Register the keep alive data as activated
keepAliveData.activated = true;

// Timeout to designate when to force the connection into reconnecting converted to milliseconds
keepAliveData.timeout = res.KeepAliveTimeout * 1000;
// Timeout to designate when to force the connection into reconnecting converted to milliseconds
keepAliveData.timeout = res.KeepAliveTimeout * 1000;

// Timeout to designate when to warn the developer that the connection may be dead or is not responding.
keepAliveData.timeoutWarning = keepAliveData.timeout * connection.keepAliveWarnAt;
// Timeout to designate when to warn the developer that the connection may be dead or is not responding.
keepAliveData.timeoutWarning = keepAliveData.timeout * connection.keepAliveWarnAt;

// Instantiate the frequency in which we check the keep alive. It must be short in order to not miss/pick up any changes
keepAliveData.checkInterval = (keepAliveData.timeout - keepAliveData.timeoutWarning) / 3;
}
else {
keepAliveData.activated = false;
}

if (!res.ProtocolVersion || res.ProtocolVersion !== "1.2") {
$(connection).triggerHandler(events.onError, ["You are using a version of the client that isn't compatible with the server. Client version 1.2, server version " + res.ProtocolVersion + "."]);
deferred.reject("You are using a version of the client that isn't compatible with the server. Client version 1.2, server version " + res.ProtocolVersion + ".");
return;
}
// Instantiate the frequency in which we check the keep alive. It must be short in order to not miss/pick up any changes
keepAliveData.checkInterval = (keepAliveData.timeout - keepAliveData.timeoutWarning) / 3;
}
else {
keepAliveData.activated = false;
}

$(connection).triggerHandler(events.onStarting);
if (!res.ProtocolVersion || res.ProtocolVersion !== "1.2") {
$(connection).triggerHandler(events.onError, ["You are using a version of the client that isn't compatible with the server. Client version 1.2, server version " + res.ProtocolVersion + "."]);
deferred.reject("You are using a version of the client that isn't compatible with the server. Client version 1.2, server version " + res.ProtocolVersion + ".");
return;
}

var transports = [],
supportedTransports = [];
$(connection).triggerHandler(events.onStarting);

$.each(signalR.transports, function (key) {
if (key === "webSockets" && !res.TryWebSockets) {
// Server said don't even try WebSockets, but keep processing the loop
return true;
}
supportedTransports.push(key);
});
var transports = [],
supportedTransports = [];

if ($.isArray(config.transport)) {
// ordered list provided
$.each(config.transport, function () {
var transport = this;
if ($.type(transport) === "object" || ($.type(transport) === "string" && $.inArray("" + transport, supportedTransports) >= 0)) {
transports.push($.type(transport) === "string" ? "" + transport : transport);
$.each(signalR.transports, function (key) {
if (key === "webSockets" && !res.TryWebSockets) {
// Server said don't even try WebSockets, but keep processing the loop
return true;
}
supportedTransports.push(key);
});
} else if ($.type(config.transport) === "object" ||
$.inArray(config.transport, supportedTransports) >= 0) {
// specific transport provided, as object or a named transport, e.g. "longPolling"
transports.push(config.transport);
} else { // default "auto"
transports = supportedTransports;

if ($.isArray(config.transport)) {
// ordered list provided
$.each(config.transport, function () {
var transport = this;
if ($.type(transport) === "object" || ($.type(transport) === "string" && $.inArray("" + transport, supportedTransports) >= 0)) {
transports.push($.type(transport) === "string" ? "" + transport : transport);
}
});
} else if ($.type(config.transport) === "object" ||
$.inArray(config.transport, supportedTransports) >= 0) {
// specific transport provided, as object or a named transport, e.g. "longPolling"
transports.push(config.transport);
} else { // default "auto"
transports = supportedTransports;
}
initialize(transports);
}
initialize(transports);
}
});
}));

return deferred.promise();
},
Expand Down
Expand Up @@ -70,27 +70,26 @@

url = this.addQs(url, connection);

$.ajax({
xhrFields: { withCredentials: connection.withCredentials },
url: url,
global: false,
cache: false,
type: "GET",
contentType: connection.contentType,
data: {},
dataType: connection.ajaxDataType,
success: function (data) {
if (data.Response === "pong") {
deferral.resolve();
$.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
xhrFields: { withCredentials: connection.withCredentials },
url: url,
type: "GET",
contentType: connection.contentType,
data: {},
dataType: connection.ajaxDataType,
success: function (data) {
if (data.Response === "pong") {
deferral.resolve();
}
else {
deferral.reject("SignalR: Invalid ping response when pinging server: " + (data.responseText || data.statusText));
}
},
error: function (data) {
deferral.reject("SignalR: Error pinging server: " + (data.responseText || data.statusText));
}
else {
deferral.reject("SignalR: Invalid ping response when pinging server: " + (data.responseText || data.statusText));
}
},
error: function (data) {
deferral.reject("SignalR: Error pinging server: " + (data.responseText || data.statusText));
}
});
}));

return deferral.promise();
},
Expand Down Expand Up @@ -174,31 +173,31 @@
ajaxSend: function (connection, data) {
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = this.addQs(url, connection);
return $.ajax({
xhrFields: { withCredentials: connection.withCredentials },
url: url,
global: false,
type: connection.ajaxDataType === "jsonp" ? "GET" : "POST",
contentType: signalR._.defaultContentType,
dataType: connection.ajaxDataType,
data: {
data: data
},
success: function (result) {
if (result) {
$(connection).triggerHandler(events.onReceived, [result]);
return $.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
xhrFields: { withCredentials: connection.withCredentials },
url: url,
type: connection.ajaxDataType === "jsonp" ? "GET" : "POST",
contentType: signalR._.defaultContentType,
dataType: connection.ajaxDataType,
data: {
data: data
},
success: function (result) {
if (result) {
$(connection).triggerHandler(events.onReceived, [result]);
}
},
error: function (errData, textStatus) {
if (textStatus === "abort" || textStatus === "parsererror") {
// The parsererror happens for sends that don't return any data, and hence
// don't write the jsonp callback to the response. This is harder to fix on the server
// so just hack around it on the client for now.
return;
}
$(connection).triggerHandler(events.onError, [errData, data]);
}
},
error: function (errData, textStatus) {
if (textStatus === "abort" || textStatus === "parsererror") {
// The parsererror happens for sends that don't return any data, and hence
// don't write the jsonp callback to the response. This is harder to fix on the server
// so just hack around it on the client for now.
return;
}
$(connection).triggerHandler(events.onError, [errData, data]);
}
});
}));
},

ajaxAbort: function (connection, async) {
Expand All @@ -211,17 +210,17 @@

var url = connection.url + "/abort" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = this.addQs(url, connection);
$.ajax({
xhrFields: { withCredentials: connection.withCredentials },
url: url,
async: async,
timeout: 1000,
global: false,
type: "POST",
contentType: connection.contentType,
dataType: connection.ajaxDataType,
data: {}
});
$.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
xhrFields: { withCredentials: connection.withCredentials },
url: url,
async: async,
timeout: 1000,
type: "POST",
contentType: connection.contentType,
dataType: connection.ajaxDataType,
data: {}
}));

connection.log("Fired ajax abort async = " + async);
},
Expand Down

0 comments on commit e3ffaf1

Please sign in to comment.