You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2609 lines
108 KiB

  1. /* jquery.signalR.core.js */
  2. /*global window:false */
  3. /*!
  4. * ASP.NET SignalR JavaScript Library v1.2.2
  5. * http://signalr.net/
  6. *
  7. * Copyright Microsoft Open Technologies, Inc. All rights reserved.
  8. * Licensed under the Apache 2.0
  9. * https://github.com/SignalR/SignalR/blob/master/LICENSE.md
  10. *
  11. */
  12. /// <reference path="Scripts/jquery-1.6.4.js" />
  13. (function ($, window, undefined) {
  14. "use strict";
  15. if (typeof ($) !== "function") {
  16. // no jQuery!
  17. throw new Error("SignalR: jQuery not found. Please ensure jQuery is referenced before the SignalR.js file.");
  18. }
  19. if (!window.JSON) {
  20. // no JSON!
  21. throw new Error("SignalR: No JSON parser found. Please ensure json2.js is referenced before the SignalR.js file if you need to support clients without native JSON parsing support, e.g. IE<8.");
  22. }
  23. var signalR,
  24. _connection,
  25. _pageLoaded = (window.document.readyState === "complete"),
  26. _pageWindow = $(window),
  27. events = {
  28. onStart: "onStart",
  29. onStarting: "onStarting",
  30. onReceived: "onReceived",
  31. onError: "onError",
  32. onConnectionSlow: "onConnectionSlow",
  33. onReconnecting: "onReconnecting",
  34. onReconnect: "onReconnect",
  35. onStateChanged: "onStateChanged",
  36. onDisconnect: "onDisconnect"
  37. },
  38. ajaxDefaults = {
  39. processData: true,
  40. timeout: null,
  41. async: true,
  42. global: false,
  43. cache: false
  44. },
  45. log = function (msg, logging) {
  46. if (logging === false) {
  47. return;
  48. }
  49. var m;
  50. if (typeof (window.console) === "undefined") {
  51. return;
  52. }
  53. m = "[" + new Date().toTimeString() + "] SignalR: " + msg;
  54. if (window.console.debug) {
  55. window.console.debug(m);
  56. } else if (window.console.log) {
  57. window.console.log(m);
  58. }
  59. },
  60. changeState = function (connection, expectedState, newState) {
  61. if (expectedState === connection.state) {
  62. connection.state = newState;
  63. $(connection).triggerHandler(events.onStateChanged, [{ oldState: expectedState, newState: newState }]);
  64. return true;
  65. }
  66. return false;
  67. },
  68. isDisconnecting = function (connection) {
  69. return connection.state === signalR.connectionState.disconnected;
  70. },
  71. configureStopReconnectingTimeout = function (connection) {
  72. var stopReconnectingTimeout,
  73. onReconnectTimeout;
  74. // Check if this connection has already been configured to stop reconnecting after a specified timeout.
  75. // Without this check if a connection is stopped then started events will be bound multiple times.
  76. if (!connection._.configuredStopReconnectingTimeout) {
  77. onReconnectTimeout = function (connection) {
  78. connection.log("Couldn't reconnect within the configured timeout (" + connection.disconnectTimeout + "ms), disconnecting.");
  79. connection.stop(/* async */ false, /* notifyServer */ false);
  80. };
  81. connection.reconnecting(function () {
  82. var connection = this;
  83. // Guard against state changing in a previous user defined even handler
  84. if (connection.state === signalR.connectionState.reconnecting) {
  85. stopReconnectingTimeout = window.setTimeout(function () { onReconnectTimeout(connection); }, connection.disconnectTimeout);
  86. }
  87. });
  88. connection.stateChanged(function (data) {
  89. if (data.oldState === signalR.connectionState.reconnecting) {
  90. // Clear the pending reconnect timeout check
  91. window.clearTimeout(stopReconnectingTimeout);
  92. }
  93. });
  94. connection._.configuredStopReconnectingTimeout = true;
  95. }
  96. };
  97. signalR = function (url, qs, logging) {
  98. /// <summary>Creates a new SignalR connection for the given url</summary>
  99. /// <param name="url" type="String">The URL of the long polling endpoint</param>
  100. /// <param name="qs" type="Object">
  101. /// [Optional] Custom querystring parameters to add to the connection URL.
  102. /// If an object, every non-function member will be added to the querystring.
  103. /// If a string, it's added to the QS as specified.
  104. /// </param>
  105. /// <param name="logging" type="Boolean">
  106. /// [Optional] A flag indicating whether connection logging is enabled to the browser
  107. /// console/log. Defaults to false.
  108. /// </param>
  109. return new signalR.fn.init(url, qs, logging);
  110. };
  111. signalR._ = {
  112. defaultContentType: "application/x-www-form-urlencoded; charset=UTF-8",
  113. ieVersion: (function () {
  114. var version,
  115. matches;
  116. if (window.navigator.appName === 'Microsoft Internet Explorer') {
  117. // Check if the user agent has the pattern "MSIE (one or more numbers).(one or more numbers)";
  118. matches = /MSIE ([0-9]+\.[0-9]+)/.exec(window.navigator.userAgent);
  119. if (matches) {
  120. version = window.parseFloat(matches[1]);
  121. }
  122. }
  123. // undefined value means not IE
  124. return version;
  125. })(),
  126. firefoxMajorVersion: function (userAgent) {
  127. // Firefox user agents: http://useragentstring.com/pages/Firefox/
  128. var matches = userAgent.match(/Firefox\/(\d+)/);
  129. if (!matches || !matches.length || matches.length < 2) {
  130. return 0;
  131. }
  132. return parseInt(matches[1], 10 /* radix */);
  133. },
  134. configurePingInterval: function (connection) {
  135. var privateData = connection._,
  136. onFail = function (error) {
  137. $(connection).triggerHandler(events.onError, [error]);
  138. };
  139. if (!privateData.pingIntervalId && privateData.pingInterval) {
  140. privateData.pingIntervalId = window.setInterval(function () {
  141. signalR.transports._logic.pingServer(connection).fail(onFail);
  142. }, privateData.pingInterval);
  143. }
  144. }
  145. };
  146. signalR.events = events;
  147. signalR.ajaxDefaults = ajaxDefaults;
  148. signalR.changeState = changeState;
  149. signalR.isDisconnecting = isDisconnecting;
  150. signalR.connectionState = {
  151. connecting: 0,
  152. connected: 1,
  153. reconnecting: 2,
  154. disconnected: 4
  155. };
  156. signalR.hub = {
  157. start: function () {
  158. // This will get replaced with the real hub connection start method when hubs is referenced correctly
  159. throw new Error("SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>.");
  160. }
  161. };
  162. _pageWindow.load(function () { _pageLoaded = true; });
  163. function validateTransport(requestedTransport, connection) {
  164. /// <summary>Validates the requested transport by cross checking it with the pre-defined signalR.transports</summary>
  165. /// <param name="requestedTransport" type="Object">The designated transports that the user has specified.</param>
  166. /// <param name="connection" type="signalR">The connection that will be using the requested transports. Used for logging purposes.</param>
  167. /// <returns type="Object" />
  168. if ($.isArray(requestedTransport)) {
  169. // Go through transport array and remove an "invalid" tranports
  170. for (var i = requestedTransport.length - 1; i >= 0; i--) {
  171. var transport = requestedTransport[i];
  172. if ($.type(requestedTransport) !== "object" && ($.type(transport) !== "string" || !signalR.transports[transport])) {
  173. connection.log("Invalid transport: " + transport + ", removing it from the transports list.");
  174. requestedTransport.splice(i, 1);
  175. }
  176. }
  177. // Verify we still have transports left, if we dont then we have invalid transports
  178. if (requestedTransport.length === 0) {
  179. connection.log("No transports remain within the specified transport array.");
  180. requestedTransport = null;
  181. }
  182. } else if ($.type(requestedTransport) !== "object" && !signalR.transports[requestedTransport] && requestedTransport !== "auto") {
  183. connection.log("Invalid transport: " + requestedTransport.toString() + ".");
  184. requestedTransport = null;
  185. }
  186. else if (requestedTransport === "auto" && signalR._.ieVersion <= 8) {
  187. // If we're doing an auto transport and we're IE8 then force longPolling, #1764
  188. return ["longPolling"];
  189. }
  190. return requestedTransport;
  191. }
  192. function getDefaultPort(protocol) {
  193. if (protocol === "http:") {
  194. return 80;
  195. }
  196. else if (protocol === "https:") {
  197. return 443;
  198. }
  199. }
  200. function addDefaultPort(protocol, url) {
  201. // Remove ports from url. We have to check if there's a / or end of line
  202. // following the port in order to avoid removing ports such as 8080.
  203. if (url.match(/:\d+$/)) {
  204. return url;
  205. } else {
  206. return url + ":" + getDefaultPort(protocol);
  207. }
  208. }
  209. signalR.fn = signalR.prototype = {
  210. init: function (url, qs, logging) {
  211. this.url = url;
  212. this.qs = qs;
  213. this._ = {
  214. keepAliveData: {},
  215. negotiateAbortText: "__Negotiate Aborted__",
  216. pingAbortText: "__Ping Aborted__",
  217. pingIntervalId: null,
  218. pingInterval: 300000,
  219. pollTimeoutId: null,
  220. reconnectTimeoutId: null,
  221. lastMessageAt: new Date().getTime(),
  222. lastActiveAt: new Date().getTime(),
  223. beatInterval: 5000, // Default value, will only be overridden if keep alive is enabled
  224. beatHandle: null,
  225. activePings: {},
  226. nextPingId: 0
  227. };
  228. if (typeof (logging) === "boolean") {
  229. this.logging = logging;
  230. }
  231. },
  232. _parseResponse: function (response) {
  233. var self = this;
  234. if (!response) {
  235. return response;
  236. } else if (self.ajaxDataType === "text") {
  237. return self.json.parse(response);
  238. } else {
  239. return response;
  240. }
  241. },
  242. json: window.JSON,
  243. isCrossDomain: function (url, against) {
  244. /// <summary>Checks if url is cross domain</summary>
  245. /// <param name="url" type="String">The base URL</param>
  246. /// <param name="against" type="Object">
  247. /// An optional argument to compare the URL against, if not specified it will be set to window.location.
  248. /// If specified it must contain a protocol and a host property.
  249. /// </param>
  250. var link;
  251. url = $.trim(url);
  252. against = against || window.location;
  253. if (url.indexOf("http") !== 0) {
  254. return false;
  255. }
  256. // Create an anchor tag.
  257. link = window.document.createElement("a");
  258. link.href = url;
  259. // When checking for cross domain we have to special case port 80 because the window.location will remove the
  260. return link.protocol + addDefaultPort(link.protocol, link.host) !== against.protocol + addDefaultPort(against.protocol, against.host);
  261. },
  262. ajaxDataType: "text",
  263. contentType: "application/json; charset=UTF-8",
  264. logging: false,
  265. state: signalR.connectionState.disconnected,
  266. reconnectDelay: 2000,
  267. disconnectTimeout: 30000, // This should be set by the server in response to the negotiate request (30s default)
  268. reconnectWindow: 30000, // This should be set by the server in response to the negotiate request
  269. keepAliveWarnAt: 2 / 3, // Warn user of slow connection if we breach the X% mark of the keep alive timeout
  270. start: function (options, callback) {
  271. /// <summary>Starts the connection</summary>
  272. /// <param name="options" type="Object">Options map</param>
  273. /// <param name="callback" type="Function">A callback function to execute when the connection has started</param>
  274. var connection = this,
  275. config = {
  276. pingInterval: 300000,
  277. waitForPageLoad: true,
  278. transport: "auto",
  279. jsonp: false
  280. },
  281. initialize,
  282. deferred = connection._deferral || $.Deferred(), // Check to see if there is a pre-existing deferral that's being built on, if so we want to keep using it
  283. parser = window.document.createElement("a");
  284. // Persist the deferral so that if start is called multiple times the same deferral is used.
  285. connection._deferral = deferred;
  286. if ($.type(options) === "function") {
  287. // Support calling with single callback parameter
  288. callback = options;
  289. } else if ($.type(options) === "object") {
  290. $.extend(config, options);
  291. if ($.type(config.callback) === "function") {
  292. callback = config.callback;
  293. }
  294. }
  295. config.transport = validateTransport(config.transport, connection);
  296. // If the transport is invalid throw an error and abort start
  297. if (!config.transport) {
  298. throw new Error("SignalR: Invalid transport(s) specified, aborting start.");
  299. }
  300. connection._.config = config;
  301. connection._.pingInterval = config.pingInterval;
  302. // Check to see if start is being called prior to page load
  303. // If waitForPageLoad is true we then want to re-direct function call to the window load event
  304. if (!_pageLoaded && config.waitForPageLoad === true) {
  305. connection._.deferredStartHandler = function () {
  306. connection.start(options, callback);
  307. };
  308. _pageWindow.bind("load", connection._.deferredStartHandler);
  309. return deferred.promise();
  310. }
  311. // If we're already connecting just return the same deferral as the original connection start
  312. if (connection.state === signalR.connectionState.connecting) {
  313. return deferred.promise();
  314. }
  315. else if (changeState(connection,
  316. signalR.connectionState.disconnected,
  317. signalR.connectionState.connecting) === false) {
  318. // We're not connecting so try and transition into connecting.
  319. // If we fail to transition then we're either in connected or reconnecting.
  320. deferred.resolve(connection);
  321. return deferred.promise();
  322. }
  323. configureStopReconnectingTimeout(connection);
  324. // Resolve the full url
  325. parser.href = connection.url;
  326. if (!parser.protocol || parser.protocol === ":") {
  327. connection.protocol = window.document.location.protocol;
  328. connection.host = window.document.location.host;
  329. connection.baseUrl = connection.protocol + "//" + connection.host;
  330. }
  331. else {
  332. connection.protocol = parser.protocol;
  333. connection.host = parser.host;
  334. connection.baseUrl = parser.protocol + "//" + parser.host;
  335. }
  336. // Set the websocket protocol
  337. connection.wsProtocol = connection.protocol === "https:" ? "wss://" : "ws://";
  338. // If jsonp with no/auto transport is specified, then set the transport to long polling
  339. // since that is the only transport for which jsonp really makes sense.
  340. // Some developers might actually choose to specify jsonp for same origin requests
  341. // as demonstrated by Issue #623.
  342. if (config.transport === "auto" && config.jsonp === true) {
  343. config.transport = "longPolling";
  344. }
  345. // If the url is protocol relative, prepend the current windows protocol to the url.
  346. if (connection.url.indexOf("//") === 0) {
  347. connection.url = window.location.protocol + connection.url;
  348. connection.log("Protocol relative URL detected, normalizing it to '" + connection.url + "'.");
  349. }
  350. if (this.isCrossDomain(connection.url)) {
  351. connection.log("Auto detected cross domain url.");
  352. if (config.transport === "auto") {
  353. // Try webSockets and longPolling since SSE doesn't support CORS
  354. // TODO: Support XDM with foreverFrame
  355. config.transport = ["webSockets", "longPolling"];
  356. }
  357. if (config.withCredentials === undefined) {
  358. config.withCredentials = true;
  359. }
  360. // Determine if jsonp is the only choice for negotiation, ajaxSend and ajaxAbort.
  361. // i.e. if the browser doesn't supports CORS
  362. // If it is, ignore any preference to the contrary, and switch to jsonp.
  363. if (!config.jsonp) {
  364. config.jsonp = !$.support.cors;
  365. if (config.jsonp) {
  366. connection.log("Using jsonp because this browser doesn't support CORS.");
  367. }
  368. }
  369. connection.contentType = signalR._.defaultContentType;
  370. }
  371. connection.withCredentials = config.withCredentials;
  372. connection.ajaxDataType = config.jsonp ? "jsonp" : "text";
  373. $(connection).bind(events.onStart, function (e, data) {
  374. if ($.type(callback) === "function") {
  375. callback.call(connection);
  376. }
  377. deferred.resolve(connection);
  378. });
  379. initialize = function (transports, index) {
  380. index = index || 0;
  381. if (index >= transports.length) {
  382. // No transport initialized successfully
  383. $(connection).triggerHandler(events.onError, ["SignalR: No transport could be initialized successfully. Try specifying a different transport or none at all for auto initialization."]);
  384. deferred.reject("SignalR: No transport could be initialized successfully. Try specifying a different transport or none at all for auto initialization.");
  385. // Stop the connection if it has connected and move it into the disconnected state
  386. connection.stop();
  387. return;
  388. }
  389. // The connection was aborted
  390. if (connection.state === signalR.connectionState.disconnected) {
  391. return;
  392. }
  393. var transportName = transports[index],
  394. transport = $.type(transportName) === "object" ? transportName : signalR.transports[transportName];
  395. connection.transport = transport;
  396. if (transportName.indexOf("_") === 0) {
  397. // Private member
  398. initialize(transports, index + 1);
  399. return;
  400. }
  401. transport.start(connection, function () { // success
  402. // Firefox 11+ doesn't allow sync XHR withCredentials: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#withCredentials
  403. var isFirefox11OrGreater = signalR._.firefoxMajorVersion(window.navigator.userAgent) >= 11,
  404. asyncAbort = !!connection.withCredentials && isFirefox11OrGreater;
  405. // The connection was aborted while initializing transports
  406. if (connection.state === signalR.connectionState.disconnected) {
  407. return;
  408. }
  409. if (transport.supportsKeepAlive && connection._.keepAliveData.activated) {
  410. signalR.transports._logic.monitorKeepAlive(connection);
  411. }
  412. signalR.transports._logic.startHeartbeat(connection);
  413. // Used to ensure low activity clients maintain their authentication.
  414. // Must be configured once a transport has been decided to perform valid ping requests.
  415. signalR._.configurePingInterval(connection);
  416. changeState(connection,
  417. signalR.connectionState.connecting,
  418. signalR.connectionState.connected);
  419. $(connection).triggerHandler(events.onStart);
  420. // wire the stop handler for when the user leaves the page
  421. _pageWindow.bind("unload", function () {
  422. connection.log("Window unloading, stopping the connection.");
  423. connection.stop(asyncAbort);
  424. });
  425. if (signalR._.firefoxMajorVersion(window.navigator.userAgent) >= 11) {
  426. _pageWindow.bind("beforeunload", function () {
  427. // If connection.stop() runs in beforeunload and fails, it will also fail
  428. // in unload unless connection.stop() runs after a timeout.
  429. window.setTimeout(function () {
  430. connection.stop(asyncAbort);
  431. }, 0);
  432. });
  433. }
  434. }, function () {
  435. initialize(transports, index + 1);
  436. });
  437. };
  438. $(connection).triggerHandler(events.onStarting);
  439. var url = connection.url + "/negotiate",
  440. onFailed = function (error, connection) {
  441. $(connection).triggerHandler(events.onError, [error.responseText]);
  442. deferred.reject("SignalR: Error during negotiation request: " + error.responseText);
  443. // Stop the connection if negotiate failed
  444. connection.stop();
  445. };
  446. url = signalR.transports._logic.prepareQueryString(connection, url);
  447. connection.log("Negotiating with '" + url + "'.");
  448. // Save the ajax negotiate request object so we can abort it if stop is called while the request is in flight.
  449. connection._.negotiateRequest = $.ajax(
  450. $.extend({}, $.signalR.ajaxDefaults, {
  451. xhrFields: { withCredentials: connection.withCredentials },
  452. url: url,
  453. type: "GET",
  454. contentType: connection.contentType,
  455. data: {},
  456. dataType: connection.ajaxDataType,
  457. error: function (error, statusText) {
  458. // We don't want to cause any errors if we're aborting our own negotiate request.
  459. if (statusText !== connection._.negotiateAbortText) {
  460. onFailed(error, connection);
  461. } else {
  462. // This rejection will noop if the deferred has already been resolved or rejected.
  463. deferred.reject("Stopped the connection while negotiating.");
  464. }
  465. },
  466. success: function (result) {
  467. var res,
  468. keepAliveData = connection._.keepAliveData;
  469. try {
  470. res = connection._parseResponse(result);
  471. }
  472. catch (error) {
  473. onFailed(error, connection);
  474. return;
  475. }
  476. keepAliveData = connection._.keepAliveData;
  477. connection.appRelativeUrl = res.Url;
  478. connection.id = res.ConnectionId;
  479. connection.token = res.ConnectionToken;
  480. connection.webSocketServerUrl = res.WebSocketServerUrl;
  481. // Once the server has labeled the PersistentConnection as Disconnected, we should stop attempting to reconnect
  482. // after res.DisconnectTimeout seconds.
  483. connection.disconnectTimeout = res.DisconnectTimeout * 1000; // in ms
  484. // If we have a keep alive
  485. if (res.KeepAliveTimeout) {
  486. // Register the keep alive data as activated
  487. keepAliveData.activated = true;
  488. // Timeout to designate when to force the connection into reconnecting converted to milliseconds
  489. keepAliveData.timeout = res.KeepAliveTimeout * 1000;
  490. // Timeout to designate when to warn the developer that the connection may be dead or is not responding.
  491. keepAliveData.timeoutWarning = keepAliveData.timeout * connection.keepAliveWarnAt;
  492. // Instantiate the frequency in which we check the keep alive. It must be short in order to not miss/pick up any changes
  493. connection._.beatInterval = (keepAliveData.timeout - keepAliveData.timeoutWarning) / 3;
  494. }
  495. else {
  496. keepAliveData.activated = false;
  497. }
  498. if (!res.ProtocolVersion || res.ProtocolVersion !== "1.2") {
  499. $(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 + "."]);
  500. 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 + ".");
  501. return;
  502. }
  503. var transports = [],
  504. supportedTransports = [];
  505. $.each(signalR.transports, function (key) {
  506. if (key === "webSockets" && !res.TryWebSockets) {
  507. // Server said don't even try WebSockets, but keep processing the loop
  508. return true;
  509. }
  510. supportedTransports.push(key);
  511. });
  512. if ($.isArray(config.transport)) {
  513. // ordered list provided
  514. $.each(config.transport, function () {
  515. var transport = this;
  516. if ($.type(transport) === "object" || ($.type(transport) === "string" && $.inArray("" + transport, supportedTransports) >= 0)) {
  517. transports.push($.type(transport) === "string" ? "" + transport : transport);
  518. }
  519. });
  520. } else if ($.type(config.transport) === "object" ||
  521. $.inArray(config.transport, supportedTransports) >= 0) {
  522. // specific transport provided, as object or a named transport, e.g. "longPolling"
  523. transports.push(config.transport);
  524. } else { // default "auto"
  525. transports = supportedTransports;
  526. }
  527. initialize(transports);
  528. }
  529. }));
  530. return deferred.promise();
  531. },
  532. starting: function (callback) {
  533. /// <summary>Adds a callback that will be invoked before anything is sent over the connection</summary>
  534. /// <param name="callback" type="Function">A callback function to execute before the connection is fully instantiated.</param>
  535. /// <returns type="signalR" />
  536. var connection = this;
  537. $(connection).bind(events.onStarting, function (e, data) {
  538. callback.call(connection);
  539. });
  540. return connection;
  541. },
  542. send: function (data) {
  543. /// <summary>Sends data over the connection</summary>
  544. /// <param name="data" type="String">The data to send over the connection</param>
  545. /// <returns type="signalR" />
  546. var connection = this;
  547. if (connection.state === signalR.connectionState.disconnected) {
  548. // Connection hasn't been started yet
  549. throw new Error("SignalR: Connection must be started before data can be sent. Call .start() before .send()");
  550. }
  551. if (connection.state === signalR.connectionState.connecting) {
  552. // Connection hasn't been started yet
  553. throw new Error("SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started.");
  554. }
  555. connection.transport.send(connection, data);
  556. // REVIEW: Should we return deferred here?
  557. return connection;
  558. },
  559. received: function (callback) {
  560. /// <summary>Adds a callback that will be invoked after anything is received over the connection</summary>
  561. /// <param name="callback" type="Function">A callback function to execute when any data is received on the connection</param>
  562. /// <returns type="signalR" />
  563. var connection = this;
  564. $(connection).bind(events.onReceived, function (e, data) {
  565. callback.call(connection, data);
  566. });
  567. return connection;
  568. },
  569. stateChanged: function (callback) {
  570. /// <summary>Adds a callback that will be invoked when the connection state changes</summary>
  571. /// <param name="callback" type="Function">A callback function to execute when the connection state changes</param>
  572. /// <returns type="signalR" />
  573. var connection = this;
  574. $(connection).bind(events.onStateChanged, function (e, data) {
  575. callback.call(connection, data);
  576. });
  577. return connection;
  578. },
  579. error: function (callback) {
  580. /// <summary>Adds a callback that will be invoked after an error occurs with the connection</summary>
  581. /// <param name="callback" type="Function">A callback function to execute when an error occurs on the connection</param>
  582. /// <returns type="signalR" />
  583. var connection = this;
  584. $(connection).bind(events.onError, function (e, errorData, sendData) {
  585. // In practice 'errorData' is the SignalR built error object.
  586. // In practice 'sendData' is undefined for all error events except those triggered by ajaxSend. For ajaxSend 'sendData' is the original send payload.
  587. callback.call(connection, errorData, sendData);
  588. });
  589. return connection;
  590. },
  591. disconnected: function (callback) {
  592. /// <summary>Adds a callback that will be invoked when the client disconnects</summary>
  593. /// <param name="callback" type="Function">A callback function to execute when the connection is broken</param>
  594. /// <returns type="signalR" />
  595. var connection = this;
  596. $(connection).bind(events.onDisconnect, function (e, data) {
  597. callback.call(connection);
  598. });
  599. return connection;
  600. },
  601. connectionSlow: function (callback) {
  602. /// <summary>Adds a callback that will be invoked when the client detects a slow connection</summary>
  603. /// <param name="callback" type="Function">A callback function to execute when the connection is slow</param>
  604. /// <returns type="signalR" />
  605. var connection = this;
  606. $(connection).bind(events.onConnectionSlow, function (e, data) {
  607. callback.call(connection);
  608. });
  609. return connection;
  610. },
  611. reconnecting: function (callback) {
  612. /// <summary>Adds a callback that will be invoked when the underlying transport begins reconnecting</summary>
  613. /// <param name="callback" type="Function">A callback function to execute when the connection enters a reconnecting state</param>
  614. /// <returns type="signalR" />
  615. var connection = this;
  616. $(connection).bind(events.onReconnecting, function (e, data) {
  617. callback.call(connection);
  618. });
  619. return connection;
  620. },
  621. reconnected: function (callback) {
  622. /// <summary>Adds a callback that will be invoked when the underlying transport reconnects</summary>
  623. /// <param name="callback" type="Function">A callback function to execute when the connection is restored</param>
  624. /// <returns type="signalR" />
  625. var connection = this;
  626. $(connection).bind(events.onReconnect, function (e, data) {
  627. callback.call(connection);
  628. });
  629. return connection;
  630. },
  631. stop: function (async, notifyServer) {
  632. /// <summary>Stops listening</summary>
  633. /// <param name="async" type="Boolean">Whether or not to asynchronously abort the connection</param>
  634. /// <param name="notifyServer" type="Boolean">Whether we want to notify the server that we are aborting the connection</param>
  635. /// <returns type="signalR" />
  636. var connection = this,
  637. // Save deferral because this is always cleaned up
  638. deferral = connection._deferral,
  639. config = connection._.config;
  640. // Verify that we've bound a load event.
  641. if (connection._.deferredStartHandler) {
  642. // Unbind the event.
  643. _pageWindow.unbind("load", connection._.deferredStartHandler);
  644. }
  645. // Always clean up private non-timeout based state.
  646. delete connection._deferral;
  647. delete connection._.config;
  648. delete connection._.deferredStartHandler;
  649. // This needs to be checked despite the connection state because a connection start can be deferred until page load.
  650. // If we've deferred the start due to a page load we need to unbind the "onLoad" -> start event.
  651. if (!_pageLoaded && (!config || config.waitForPageLoad === true)) {
  652. connection.log("Stopping connection prior to negotiate.");
  653. // If we have a deferral we should reject it
  654. if (deferral) {
  655. deferral.reject("The connection was stopped during page load.");
  656. }
  657. // Short-circuit because the start has not been fully started.
  658. return;
  659. }
  660. if (connection.state === signalR.connectionState.disconnected) {
  661. return;
  662. }
  663. connection.log("Stopping connection.");
  664. changeState(connection, connection.state, signalR.connectionState.disconnected);
  665. window.clearTimeout(connection._.beatHandle);
  666. window.clearInterval(connection._.pingIntervalId);
  667. window.clearTimeout(connection._.pingLoopId);
  668. if (connection.transport) {
  669. if (notifyServer !== false) {
  670. connection.transport.abort(connection, async);
  671. }
  672. if (connection.transport.supportsKeepAlive && connection._.keepAliveData.activated) {
  673. signalR.transports._logic.stopMonitoringKeepAlive(connection);
  674. }
  675. connection.transport.stop(connection);
  676. connection.transport = null;
  677. }
  678. if (connection._.negotiateRequest) {
  679. // If the negotiation request has already completed this will noop.
  680. connection._.negotiateRequest.abort(connection._.negotiateAbortText);
  681. delete connection._.negotiateRequest;
  682. }
  683. $.each(connection._.activePings, function (i, pingXhr) {
  684. connection.log("Aborting ping " + i + ".");
  685. pingXhr.abort(connection._.pingAbortText);
  686. });
  687. // Trigger the disconnect event
  688. $(connection).triggerHandler(events.onDisconnect);
  689. delete connection.messageId;
  690. delete connection.groupsToken;
  691. delete connection.id;
  692. delete connection._.pingIntervalId;
  693. delete connection._.lastMessageAt;
  694. delete connection._.lastActiveAt;
  695. delete connection._.pingLoopId;
  696. return connection;
  697. },
  698. log: function (msg) {
  699. log(msg, this.logging);
  700. }
  701. };
  702. signalR.fn.init.prototype = signalR.fn;
  703. signalR.noConflict = function () {
  704. /// <summary>Reinstates the original value of $.connection and returns the signalR object for manual assignment</summary>
  705. /// <returns type="signalR" />
  706. if ($.connection === signalR) {
  707. $.connection = _connection;
  708. }
  709. return signalR;
  710. };
  711. if ($.connection) {
  712. _connection = $.connection;
  713. }
  714. $.connection = $.signalR = signalR;
  715. }(window.jQuery, window));
  716. /* jquery.signalR.transports.common.js */
  717. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  718. /*global window:false */
  719. /// <reference path="jquery.signalR.core.js" />
  720. (function ($, window, undefined) {
  721. "use strict";
  722. var signalR = $.signalR,
  723. events = $.signalR.events,
  724. changeState = $.signalR.changeState,
  725. transportLogic;
  726. signalR.transports = {};
  727. function beat(connection) {
  728. if (connection._.keepAliveData.monitoring) {
  729. checkIfAlive(connection);
  730. }
  731. // Ensure that we successfully marked active before continuing the heartbeat.
  732. if (transportLogic.markActive(connection)) {
  733. connection._.beatHandle = window.setTimeout(function () {
  734. beat(connection);
  735. }, connection._.beatInterval);
  736. }
  737. }
  738. function checkIfAlive(connection) {
  739. var keepAliveData = connection._.keepAliveData,
  740. timeElapsed;
  741. // Only check if we're connected
  742. if (connection.state === signalR.connectionState.connected) {
  743. timeElapsed = new Date().getTime() - connection._.lastMessageAt;
  744. // Check if the keep alive has completely timed out
  745. if (timeElapsed >= keepAliveData.timeout) {
  746. connection.log("Keep alive timed out. Notifying transport that connection has been lost.");
  747. // Notify transport that the connection has been lost
  748. connection.transport.lostConnection(connection);
  749. }
  750. else if (timeElapsed >= keepAliveData.timeoutWarning) {
  751. // This is to assure that the user only gets a single warning
  752. if (!keepAliveData.userNotified) {
  753. connection.log("Keep alive has been missed, connection may be dead/slow.");
  754. $(connection).triggerHandler(events.onConnectionSlow);
  755. keepAliveData.userNotified = true;
  756. }
  757. }
  758. else {
  759. keepAliveData.userNotified = false;
  760. }
  761. }
  762. }
  763. function isConnectedOrReconnecting(connection) {
  764. return connection.state === signalR.connectionState.connected ||
  765. connection.state === signalR.connectionState.reconnecting;
  766. }
  767. function addConnectionData(url, connectionData) {
  768. var appender = url.indexOf("?") !== -1 ? "&" : "?";
  769. if (connectionData) {
  770. url += appender + "connectionData=" + window.encodeURIComponent(connectionData);
  771. }
  772. return url;
  773. }
  774. transportLogic = signalR.transports._logic = {
  775. pingServer: function (connection) {
  776. /// <summary>Pings the server</summary>
  777. /// <param name="connection" type="signalr">Connection associated with the server ping</param>
  778. /// <returns type="signalR" />
  779. var url,
  780. deferral = $.Deferred(),
  781. activePings = connection._.activePings,
  782. pingId = connection._.nextPingId++;
  783. url = connection.url + "/ping";
  784. url = transportLogic.prepareQueryString(connection, url);
  785. activePings[pingId] = $.ajax(
  786. $.extend({}, $.signalR.ajaxDefaults, {
  787. xhrFields: { withCredentials: connection.withCredentials },
  788. url: url,
  789. type: "GET",
  790. contentType: connection.contentType,
  791. data: {},
  792. dataType: connection.ajaxDataType,
  793. success: function (result) {
  794. var data;
  795. try {
  796. data = connection._parseResponse(result);
  797. }
  798. catch (error) {
  799. deferral.reject("Failed to parse ping server response, stopping the connection: " + result);
  800. connection.stop();
  801. return;
  802. }
  803. if (data.Response === "pong") {
  804. deferral.resolve();
  805. }
  806. else {
  807. deferral.reject("SignalR: Invalid ping response when pinging server: " + data.Response);
  808. }
  809. },
  810. error: function (error, statusText) {
  811. if (error.status === 401 || error.status === 403) {
  812. deferral.reject("Failed to ping server. Server responded with a " + error.status + " status code, stopping the connection.");
  813. connection.stop();
  814. }
  815. else if (statusText === connection._.pingAbortText)
  816. {
  817. // We don't want to cause any errors if we're aborting our own ping request.
  818. // Don't reject or resolve the deferred, because we want the long-polling pingLoop to stop.
  819. connection.log("Ping " + pingId + " aborted.");
  820. }
  821. else {
  822. deferral.reject("SignalR: Error pinging server: " + (error.responseText || error.statusText));
  823. }
  824. },
  825. complete: function () {
  826. delete activePings[pingId];
  827. }
  828. }));
  829. return deferral.promise();
  830. },
  831. prepareQueryString: function (connection, url) {
  832. url = transportLogic.addQs(url, connection);
  833. return addConnectionData(url, connection.data);
  834. },
  835. addQs: function (url, connection) {
  836. var appender = url.indexOf("?") !== -1 ? "&" : "?",
  837. firstChar;
  838. if (!connection.qs) {
  839. return url;
  840. }
  841. if (typeof (connection.qs) === "object") {
  842. return url + appender + $.param(connection.qs);
  843. }
  844. if (typeof (connection.qs) === "string") {
  845. firstChar = connection.qs.charAt(0);
  846. if (firstChar === "?" || firstChar === "&") {
  847. appender = "";
  848. }
  849. return url + appender + connection.qs;
  850. }
  851. throw new Error("Connections query string property must be either a string or object.");
  852. },
  853. getUrl: function (connection, transport, reconnecting, poll) {
  854. /// <summary>Gets the url for making a GET based connect request</summary>
  855. var baseUrl = transport === "webSockets" ? "" : connection.baseUrl,
  856. url = baseUrl + connection.appRelativeUrl,
  857. qs = "transport=" + transport + "&connectionToken=" + window.encodeURIComponent(connection.token);
  858. if (connection.groupsToken) {
  859. qs += "&groupsToken=" + window.encodeURIComponent(connection.groupsToken);
  860. }
  861. if (!reconnecting) {
  862. url += "/connect";
  863. } else {
  864. if (poll) {
  865. // longPolling transport specific
  866. url += "/poll";
  867. } else {
  868. url += "/reconnect";
  869. }
  870. if (connection.messageId) {
  871. qs += "&messageId=" + window.encodeURIComponent(connection.messageId);
  872. }
  873. }
  874. url += "?" + qs;
  875. url = transportLogic.prepareQueryString(connection, url);
  876. url += "&tid=" + Math.floor(Math.random() * 11);
  877. return url;
  878. },
  879. maximizePersistentResponse: function (minPersistentResponse) {
  880. return {
  881. MessageId: minPersistentResponse.C,
  882. Messages: minPersistentResponse.M,
  883. Disconnect: typeof (minPersistentResponse.D) !== "undefined" ? true : false,
  884. ShouldReconnect: typeof (minPersistentResponse.T) !== "undefined" ? true : false,
  885. LongPollDelay: minPersistentResponse.L,
  886. GroupsToken: minPersistentResponse.G
  887. };
  888. },
  889. updateGroups: function (connection, groupsToken) {
  890. if (groupsToken) {
  891. connection.groupsToken = groupsToken;
  892. }
  893. },
  894. ajaxSend: function (connection, data) {
  895. var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token),
  896. onFail = function (error, connection) {
  897. $(connection).triggerHandler(events.onError, [error, data]);
  898. };
  899. url = transportLogic.prepareQueryString(connection, url);
  900. return $.ajax(
  901. $.extend({}, $.signalR.ajaxDefaults, {
  902. xhrFields: { withCredentials: connection.withCredentials },
  903. url: url,
  904. type: connection.ajaxDataType === "jsonp" ? "GET" : "POST",
  905. contentType: signalR._.defaultContentType,
  906. dataType: connection.ajaxDataType,
  907. data: {
  908. data: data
  909. },
  910. success: function (result) {
  911. var res;
  912. if (result) {
  913. try {
  914. res = connection._parseResponse(result);
  915. }
  916. catch (error) {
  917. onFail(error, connection);
  918. connection.stop();
  919. return;
  920. }
  921. $(connection).triggerHandler(events.onReceived, [res]);
  922. }
  923. },
  924. error: function (error, textStatus) {
  925. if (textStatus === "abort" || textStatus === "parsererror") {
  926. // The parsererror happens for sends that don't return any data, and hence
  927. // don't write the jsonp callback to the response. This is harder to fix on the server
  928. // so just hack around it on the client for now.
  929. return;
  930. }
  931. onFail(error, connection);
  932. }
  933. }));
  934. },
  935. ajaxAbort: function (connection, async) {
  936. if (typeof (connection.transport) === "undefined") {
  937. return;
  938. }
  939. // Async by default unless explicitly overidden
  940. async = typeof async === "undefined" ? true : async;
  941. var url = connection.url + "/abort" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
  942. url = transportLogic.prepareQueryString(connection, url);
  943. $.ajax(
  944. $.extend({}, $.signalR.ajaxDefaults, {
  945. xhrFields: { withCredentials: connection.withCredentials },
  946. url: url,
  947. async: async,
  948. timeout: 1000,
  949. type: "POST",
  950. contentType: connection.contentType,
  951. dataType: connection.ajaxDataType,
  952. data: {}
  953. }));
  954. connection.log("Fired ajax abort async = " + async + ".");
  955. },
  956. processMessages: function (connection, minData) {
  957. var data;
  958. // Transport can be null if we've just closed the connection
  959. if (connection.transport) {
  960. var $connection = $(connection);
  961. // Update the last message time stamp
  962. transportLogic.markLastMessage(connection);
  963. if (!minData) {
  964. return;
  965. }
  966. data = this.maximizePersistentResponse(minData);
  967. if (data.Disconnect) {
  968. connection.log("Disconnect command received from server.");
  969. // Disconnected by the server
  970. connection.stop(false, false);
  971. return;
  972. }
  973. this.updateGroups(connection, data.GroupsToken);
  974. if (data.Messages) {
  975. $.each(data.Messages, function (index, message) {
  976. $connection.triggerHandler(events.onReceived, [message]);
  977. });
  978. }
  979. if (data.MessageId) {
  980. connection.messageId = data.MessageId;
  981. }
  982. }
  983. },
  984. monitorKeepAlive: function (connection) {
  985. var keepAliveData = connection._.keepAliveData;
  986. // If we haven't initiated the keep alive timeouts then we need to
  987. if (!keepAliveData.monitoring) {
  988. keepAliveData.monitoring = true;
  989. // Initialize the keep alive time stamp ping
  990. transportLogic.markLastMessage(connection);
  991. // Save the function so we can unbind it on stop
  992. connection._.keepAliveData.reconnectKeepAliveUpdate = function () {
  993. // Mark a new message so that keep alive doesn't time out connections
  994. transportLogic.markLastMessage(connection);
  995. };
  996. // Update Keep alive on reconnect
  997. $(connection).bind(events.onReconnect, connection._.keepAliveData.reconnectKeepAliveUpdate);
  998. connection.log("Now monitoring keep alive with a warning timeout of " + keepAliveData.timeoutWarning + " and a connection lost timeout of " + keepAliveData.timeout + ".");
  999. }
  1000. else {
  1001. connection.log("Tried to monitor keep alive but it's already being monitored.");
  1002. }
  1003. },
  1004. stopMonitoringKeepAlive: function (connection) {
  1005. var keepAliveData = connection._.keepAliveData;
  1006. // Only attempt to stop the keep alive monitoring if its being monitored
  1007. if (keepAliveData.monitoring) {
  1008. // Stop monitoring
  1009. keepAliveData.monitoring = false;
  1010. // Remove the updateKeepAlive function from the reconnect event
  1011. $(connection).unbind(events.onReconnect, connection._.keepAliveData.reconnectKeepAliveUpdate);
  1012. // Clear all the keep alive data
  1013. connection._.keepAliveData = {};
  1014. connection.log("Stopping the monitoring of the keep alive.");
  1015. }
  1016. },
  1017. startHeartbeat: function (connection) {
  1018. beat(connection);
  1019. },
  1020. markLastMessage: function (connection) {
  1021. connection._.lastMessageAt = new Date().getTime();
  1022. },
  1023. markActive: function (connection) {
  1024. if (transportLogic.verifyLastActive(connection)) {
  1025. connection._.lastActiveAt = new Date().getTime();
  1026. return true;
  1027. }
  1028. return false;
  1029. },
  1030. ensureReconnectingState: function (connection) {
  1031. if (changeState(connection,
  1032. signalR.connectionState.connected,
  1033. signalR.connectionState.reconnecting) === true) {
  1034. $(connection).triggerHandler(events.onReconnecting);
  1035. }
  1036. return connection.state === signalR.connectionState.reconnecting;
  1037. },
  1038. clearReconnectTimeout: function (connection) {
  1039. if (connection && connection._.reconnectTimeout) {
  1040. window.clearTimeout(connection._.reconnectTimeout);
  1041. delete connection._.reconnectTimeout;
  1042. }
  1043. },
  1044. verifyLastActive: function (connection) {
  1045. if (new Date().getTime() - connection._.lastActiveAt >= connection.reconnectWindow) {
  1046. connection.log("There has not been an active server connection for an extended periord of time. Stopping connection.");
  1047. connection.stop();
  1048. return false;
  1049. }
  1050. return true;
  1051. },
  1052. reconnect: function (connection, transportName) {
  1053. var transport = signalR.transports[transportName],
  1054. that = this;
  1055. // We should only set a reconnectTimeout if we are currently connected
  1056. // and a reconnectTimeout isn't already set.
  1057. if (isConnectedOrReconnecting(connection) && !connection._.reconnectTimeout) {
  1058. // Need to verify before the setTimeout occurs because an application sleep could occur during the setTimeout duration.
  1059. if (!transportLogic.verifyLastActive(connection)) {
  1060. return;
  1061. }
  1062. connection._.reconnectTimeout = window.setTimeout(function () {
  1063. if (!transportLogic.verifyLastActive(connection)) {
  1064. return;
  1065. }
  1066. transport.stop(connection);
  1067. if (that.ensureReconnectingState(connection)) {
  1068. connection.log(transportName + " reconnecting.");
  1069. transport.start(connection);
  1070. }
  1071. }, connection.reconnectDelay);
  1072. }
  1073. },
  1074. handleParseFailure: function (connection, result, errorMessage, onFailed) {
  1075. // If we're in the initialization phase trigger onFailed, otherwise stop the connection.
  1076. if (connection.state === signalR.connectionState.connecting) {
  1077. connection.log("Failed to parse server response while attempting to connect.");
  1078. onFailed();
  1079. }
  1080. else {
  1081. $(connection).triggerHandler(events.onError, ["SignalR: failed at parsing response: " + result + ". With error: " + errorMessage]);
  1082. connection.stop();
  1083. }
  1084. },
  1085. foreverFrame: {
  1086. count: 0,
  1087. connections: {}
  1088. }
  1089. };
  1090. }(window.jQuery, window));
  1091. /* jquery.signalR.transports.webSockets.js */
  1092. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  1093. /*global window:false */
  1094. /// <reference path="jquery.signalR.transports.common.js" />
  1095. (function ($, window, undefined) {
  1096. "use strict";
  1097. var signalR = $.signalR,
  1098. events = $.signalR.events,
  1099. changeState = $.signalR.changeState,
  1100. transportLogic = signalR.transports._logic;
  1101. signalR.transports.webSockets = {
  1102. name: "webSockets",
  1103. supportsKeepAlive: true,
  1104. timeOut: 3000,
  1105. send: function (connection, data) {
  1106. connection.socket.send(data);
  1107. },
  1108. start: function (connection, onSuccess, onFailed) {
  1109. var url,
  1110. opened = false,
  1111. that = this,
  1112. initialSocket,
  1113. timeOutHandle,
  1114. reconnecting = !onSuccess;
  1115. if (!window.WebSocket) {
  1116. onFailed();
  1117. return;
  1118. }
  1119. if (!connection.socket) {
  1120. if (connection.webSocketServerUrl) {
  1121. url = connection.webSocketServerUrl;
  1122. }
  1123. else {
  1124. url = connection.wsProtocol + connection.host;
  1125. }
  1126. url += transportLogic.getUrl(connection, this.name, reconnecting);
  1127. connection.log("Connecting to websocket endpoint '" + url + "'.");
  1128. connection.socket = new window.WebSocket(url);
  1129. // Issue #1653: Galaxy S3 Android Stock Browser fails silently to establish websocket connections.
  1130. if (onFailed) {
  1131. initialSocket = connection.socket;
  1132. timeOutHandle = window.setTimeout(function () {
  1133. if (initialSocket === connection.socket) {
  1134. connection.log("WebSocket timed out trying to connect.");
  1135. onFailed();
  1136. }
  1137. }, that.timeOut);
  1138. }
  1139. connection.socket.onopen = function () {
  1140. window.clearTimeout(timeOutHandle);
  1141. opened = true;
  1142. connection.log("Websocket opened.");
  1143. };
  1144. connection.socket.onclose = function (event) {
  1145. // Only handle a socket close if the close is from the current socket.
  1146. // Sometimes on disconnect the server will push down an onclose event
  1147. // to an expired socket.
  1148. window.clearTimeout(timeOutHandle);
  1149. if (this === connection.socket) {
  1150. if (!opened) {
  1151. if (onFailed) {
  1152. onFailed();
  1153. }
  1154. else if (reconnecting) {
  1155. that.reconnect(connection);
  1156. }
  1157. return;
  1158. }
  1159. else if (typeof event.wasClean !== "undefined" && event.wasClean === false) {
  1160. // Ideally this would use the websocket.onerror handler (rather than checking wasClean in onclose) but
  1161. // I found in some circumstances Chrome won't call onerror. This implementation seems to work on all browsers.
  1162. $(connection).triggerHandler(events.onError, [event.reason]);
  1163. connection.log("Unclean disconnect from websocket: " + event.reason || "[no reason given].");
  1164. }
  1165. else {
  1166. connection.log("Websocket closed.");
  1167. }
  1168. that.reconnect(connection);
  1169. }
  1170. };
  1171. connection.socket.onmessage = function (event) {
  1172. var data,
  1173. $connection = $(connection);
  1174. try {
  1175. data = connection._parseResponse(event.data);
  1176. }
  1177. catch (error) {
  1178. transportLogic.handleParseFailure(connection, event.data, error.message, onFailed);
  1179. return;
  1180. }
  1181. if (onSuccess) {
  1182. onSuccess();
  1183. onSuccess = null;
  1184. } else if (changeState(connection,
  1185. signalR.connectionState.reconnecting,
  1186. signalR.connectionState.connected) === true) {
  1187. $connection.triggerHandler(events.onReconnect);
  1188. }
  1189. transportLogic.clearReconnectTimeout(connection);
  1190. if (data) {
  1191. // data.M is PersistentResponse.Messages
  1192. if ($.isEmptyObject(data) || data.M) {
  1193. transportLogic.processMessages(connection, data);
  1194. } else {
  1195. // For websockets we need to trigger onReceived
  1196. // for callbacks to outgoing hub calls.
  1197. $connection.triggerHandler(events.onReceived, [data]);
  1198. }
  1199. }
  1200. };
  1201. }
  1202. },
  1203. reconnect: function (connection) {
  1204. transportLogic.reconnect(connection, this.name);
  1205. },
  1206. lostConnection: function (connection) {
  1207. this.reconnect(connection);
  1208. },
  1209. stop: function (connection) {
  1210. // Don't trigger a reconnect after stopping
  1211. transportLogic.clearReconnectTimeout(connection);
  1212. if (connection.socket !== null) {
  1213. connection.log("Closing the Websocket.");
  1214. connection.socket.close();
  1215. connection.socket = null;
  1216. }
  1217. },
  1218. abort: function (connection, async) {
  1219. transportLogic.ajaxAbort(connection, async);
  1220. }
  1221. };
  1222. }(window.jQuery, window));
  1223. /* jquery.signalR.transports.serverSentEvents.js */
  1224. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  1225. /*global window:false */
  1226. /// <reference path="jquery.signalR.transports.common.js" />
  1227. (function ($, window, undefined) {
  1228. "use strict";
  1229. var signalR = $.signalR,
  1230. events = $.signalR.events,
  1231. changeState = $.signalR.changeState,
  1232. transportLogic = signalR.transports._logic;
  1233. signalR.transports.serverSentEvents = {
  1234. name: "serverSentEvents",
  1235. supportsKeepAlive: true,
  1236. timeOut: 3000,
  1237. start: function (connection, onSuccess, onFailed) {
  1238. var that = this,
  1239. opened = false,
  1240. $connection = $(connection),
  1241. reconnecting = !onSuccess,
  1242. url,
  1243. connectTimeOut;
  1244. if (connection.eventSource) {
  1245. connection.log("The connection already has an event source. Stopping it.");
  1246. connection.stop();
  1247. }
  1248. if (!window.EventSource) {
  1249. if (onFailed) {
  1250. connection.log("This browser doesn't support SSE.");
  1251. onFailed();
  1252. }
  1253. return;
  1254. }
  1255. url = transportLogic.getUrl(connection, this.name, reconnecting);
  1256. try {
  1257. connection.log("Attempting to connect to SSE endpoint '" + url + "'.");
  1258. connection.eventSource = new window.EventSource(url);
  1259. }
  1260. catch (e) {
  1261. connection.log("EventSource failed trying to connect with error " + e.Message + ".");
  1262. if (onFailed) {
  1263. // The connection failed, call the failed callback
  1264. onFailed();
  1265. }
  1266. else {
  1267. $connection.triggerHandler(events.onError, [e]);
  1268. if (reconnecting) {
  1269. // If we were reconnecting, rather than doing initial connect, then try reconnect again
  1270. that.reconnect(connection);
  1271. }
  1272. }
  1273. return;
  1274. }
  1275. // After connecting, if after the specified timeout there's no response stop the connection
  1276. // and raise on failed
  1277. connectTimeOut = window.setTimeout(function () {
  1278. if (opened === false && connection.eventSource) {
  1279. connection.log("EventSource timed out trying to connect.");
  1280. connection.log("EventSource readyState: " + connection.eventSource.readyState + ".");
  1281. if (!reconnecting) {
  1282. that.stop(connection);
  1283. }
  1284. if (reconnecting) {
  1285. // If we're reconnecting and the event source is attempting to connect,
  1286. // don't keep retrying. This causes duplicate connections to spawn.
  1287. if (connection.eventSource.readyState !== window.EventSource.OPEN) {
  1288. // If we were reconnecting, rather than doing initial connect, then try reconnect again
  1289. that.reconnect(connection);
  1290. }
  1291. } else if (onFailed) {
  1292. onFailed();
  1293. }
  1294. }
  1295. },
  1296. that.timeOut);
  1297. connection.eventSource.addEventListener("open", function (e) {
  1298. connection.log("EventSource connected.");
  1299. if (connectTimeOut) {
  1300. window.clearTimeout(connectTimeOut);
  1301. }
  1302. transportLogic.clearReconnectTimeout(connection);
  1303. if (opened === false) {
  1304. opened = true;
  1305. if (onSuccess) {
  1306. onSuccess();
  1307. } else if (changeState(connection,
  1308. signalR.connectionState.reconnecting,
  1309. signalR.connectionState.connected) === true) {
  1310. // If there's no onSuccess handler we assume this is a reconnect
  1311. $connection.triggerHandler(events.onReconnect);
  1312. }
  1313. }
  1314. }, false);
  1315. connection.eventSource.addEventListener("message", function (e) {
  1316. var res;
  1317. // process messages
  1318. if (e.data === "initialized") {
  1319. return;
  1320. }
  1321. try {
  1322. res = connection._parseResponse(e.data);
  1323. }
  1324. catch (error) {
  1325. transportLogic.handleParseFailure(connection, e.data, error.message, onFailed);
  1326. return;
  1327. }
  1328. transportLogic.processMessages(connection, res, onSuccess);
  1329. }, false);
  1330. connection.eventSource.addEventListener("error", function (e) {
  1331. // Only handle an error if the error is from the current Event Source.
  1332. // Sometimes on disconnect the server will push down an error event
  1333. // to an expired Event Source.
  1334. if (this === connection.eventSource) {
  1335. if (!opened) {
  1336. if (onFailed) {
  1337. onFailed();
  1338. }
  1339. return;
  1340. }
  1341. connection.log("EventSource readyState: " + connection.eventSource.readyState + ".");
  1342. if (e.eventPhase === window.EventSource.CLOSED) {
  1343. // We don't use the EventSource's native reconnect function as it
  1344. // doesn't allow us to change the URL when reconnecting. We need
  1345. // to change the URL to not include the /connect suffix, and pass
  1346. // the last message id we received.
  1347. connection.log("EventSource reconnecting due to the server connection ending.");
  1348. that.reconnect(connection);
  1349. } else {
  1350. // connection error
  1351. connection.log("EventSource error.");
  1352. $connection.triggerHandler(events.onError);
  1353. }
  1354. }
  1355. }, false);
  1356. },
  1357. reconnect: function (connection) {
  1358. transportLogic.reconnect(connection, this.name);
  1359. },
  1360. lostConnection: function (connection) {
  1361. this.reconnect(connection);
  1362. },
  1363. send: function (connection, data) {
  1364. transportLogic.ajaxSend(connection, data);
  1365. },
  1366. stop: function (connection) {
  1367. // Don't trigger a reconnect after stopping
  1368. transportLogic.clearReconnectTimeout(connection);
  1369. if (connection && connection.eventSource) {
  1370. connection.log("EventSource calling close().");
  1371. connection.eventSource.close();
  1372. connection.eventSource = null;
  1373. delete connection.eventSource;
  1374. }
  1375. },
  1376. abort: function (connection, async) {
  1377. transportLogic.ajaxAbort(connection, async);
  1378. }
  1379. };
  1380. }(window.jQuery, window));
  1381. /* jquery.signalR.transports.foreverFrame.js */
  1382. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  1383. /*global window:false */
  1384. /// <reference path="jquery.signalR.transports.common.js" />
  1385. (function ($, window, undefined) {
  1386. "use strict";
  1387. var signalR = $.signalR,
  1388. events = $.signalR.events,
  1389. changeState = $.signalR.changeState,
  1390. transportLogic = signalR.transports._logic,
  1391. createFrame = function () {
  1392. var frame = window.document.createElement("iframe");
  1393. frame.setAttribute("style", "position:absolute;top:0;left:0;width:0;height:0;visibility:hidden;");
  1394. return frame;
  1395. },
  1396. // Used to prevent infinite loading icon spins in older versions of ie
  1397. // We build this object inside a closure so we don't pollute the rest of
  1398. // the foreverFrame transport with unnecessary functions/utilities.
  1399. loadPreventer = (function () {
  1400. var loadingFixIntervalId = null,
  1401. loadingFixInterval = 1000,
  1402. attachedTo = 0;
  1403. return {
  1404. prevent: function () {
  1405. // Prevent additional iframe removal procedures from newer browsers
  1406. if (signalR._.ieVersion <= 8) {
  1407. // We only ever want to set the interval one time, so on the first attachedTo
  1408. if (attachedTo === 0) {
  1409. // Create and destroy iframe every 3 seconds to prevent loading icon, super hacky
  1410. loadingFixIntervalId = window.setInterval(function () {
  1411. var tempFrame = createFrame();
  1412. window.document.body.appendChild(tempFrame);
  1413. window.document.body.removeChild(tempFrame);
  1414. tempFrame = null;
  1415. }, loadingFixInterval);
  1416. }
  1417. attachedTo++;
  1418. }
  1419. },
  1420. cancel: function () {
  1421. // Only clear the interval if there's only one more object that the loadPreventer is attachedTo
  1422. if (attachedTo === 1) {
  1423. window.clearInterval(loadingFixIntervalId);
  1424. }
  1425. if (attachedTo > 0) {
  1426. attachedTo--;
  1427. }
  1428. }
  1429. };
  1430. })();
  1431. signalR.transports.foreverFrame = {
  1432. name: "foreverFrame",
  1433. supportsKeepAlive: true,
  1434. timeOut: 3000,
  1435. // Added as a value here so we can create tests to verify functionality
  1436. iframeClearThreshold: 50,
  1437. start: function (connection, onSuccess, onFailed) {
  1438. var that = this,
  1439. frameId = (transportLogic.foreverFrame.count += 1),
  1440. url,
  1441. frame = createFrame(),
  1442. frameLoadHandler = function () {
  1443. connection.log("Forever frame iframe finished loading and is no longer receiving messages, reconnecting.");
  1444. that.reconnect(connection);
  1445. };
  1446. if (window.EventSource) {
  1447. // If the browser supports SSE, don't use Forever Frame
  1448. if (onFailed) {
  1449. connection.log("This browser supports SSE, skipping Forever Frame.");
  1450. onFailed();
  1451. }
  1452. return;
  1453. }
  1454. frame.setAttribute("data-signalr-connection-id", connection.id);
  1455. // Start preventing loading icon
  1456. // This will only perform work if the loadPreventer is not attached to another connection.
  1457. loadPreventer.prevent();
  1458. // Build the url
  1459. url = transportLogic.getUrl(connection, this.name);
  1460. url += "&frameId=" + frameId;
  1461. // Set body prior to setting URL to avoid caching issues.
  1462. window.document.body.appendChild(frame);
  1463. connection.log("Binding to iframe's load event.");
  1464. if (frame.addEventListener) {
  1465. frame.addEventListener("load", frameLoadHandler, false);
  1466. } else if (frame.attachEvent) {
  1467. frame.attachEvent("onload", frameLoadHandler);
  1468. }
  1469. frame.src = url;
  1470. transportLogic.foreverFrame.connections[frameId] = connection;
  1471. connection.frame = frame;
  1472. connection.frameId = frameId;
  1473. if (onSuccess) {
  1474. connection.onSuccess = function () {
  1475. connection.log("Iframe transport started.");
  1476. onSuccess();
  1477. };
  1478. }
  1479. // After connecting, if after the specified timeout there's no response stop the connection
  1480. // and raise on failed
  1481. window.setTimeout(function () {
  1482. if (connection.onSuccess) {
  1483. connection.log("Failed to connect using forever frame source, it timed out after " + that.timeOut + "ms.");
  1484. that.stop(connection);
  1485. if (onFailed) {
  1486. onFailed();
  1487. }
  1488. }
  1489. }, that.timeOut);
  1490. },
  1491. reconnect: function (connection) {
  1492. var that = this;
  1493. // Need to verify before the setTimeout occurs because an application sleep could occur during the setTimeout duration.
  1494. if (!transportLogic.verifyLastActive(connection)) {
  1495. return;
  1496. }
  1497. window.setTimeout(function () {
  1498. // Verify that we're ok to reconnect
  1499. if (!transportLogic.verifyLastActive(connection)) {
  1500. return;
  1501. }
  1502. if (connection.frame && transportLogic.ensureReconnectingState(connection)) {
  1503. var frame = connection.frame,
  1504. src = transportLogic.getUrl(connection, that.name, true) + "&frameId=" + connection.frameId;
  1505. connection.log("Updating iframe src to '" + src + "'.");
  1506. frame.src = src;
  1507. }
  1508. }, connection.reconnectDelay);
  1509. },
  1510. lostConnection: function (connection) {
  1511. this.reconnect(connection);
  1512. },
  1513. send: function (connection, data) {
  1514. transportLogic.ajaxSend(connection, data);
  1515. },
  1516. receive: function (connection, data) {
  1517. var cw,
  1518. body;
  1519. transportLogic.processMessages(connection, data);
  1520. // Delete the script & div elements
  1521. connection.frameMessageCount = (connection.frameMessageCount || 0) + 1;
  1522. if (connection.frameMessageCount > signalR.transports.foreverFrame.iframeClearThreshold && connection.state === $.signalR.connectionState.connected) {
  1523. connection.frameMessageCount = 0;
  1524. cw = connection.frame.contentWindow || connection.frame.contentDocument;
  1525. if (cw && cw.document && cw.document.body) {
  1526. body = cw.document.body;
  1527. // Remove all the child elements from the iframe's body to conserver memory
  1528. while (body.firstChild) {
  1529. body.removeChild(body.firstChild);
  1530. }
  1531. }
  1532. }
  1533. },
  1534. stop: function (connection) {
  1535. var cw = null;
  1536. // Stop attempting to prevent loading icon
  1537. loadPreventer.cancel();
  1538. if (connection.frame) {
  1539. if (connection.frame.stop) {
  1540. connection.frame.stop();
  1541. } else {
  1542. try {
  1543. cw = connection.frame.contentWindow || connection.frame.contentDocument;
  1544. if (cw.document && cw.document.execCommand) {
  1545. cw.document.execCommand("Stop");
  1546. }
  1547. }
  1548. catch (e) {
  1549. connection.log("Error occured when stopping foreverFrame transport. Message = " + e.message + ".");
  1550. }
  1551. }
  1552. // Ensure the iframe is where we left it
  1553. if (connection.frame.parentNode === window.document.body) {
  1554. window.document.body.removeChild(connection.frame);
  1555. }
  1556. delete transportLogic.foreverFrame.connections[connection.frameId];
  1557. connection.frame = null;
  1558. connection.frameId = null;
  1559. delete connection.frame;
  1560. delete connection.frameId;
  1561. delete connection.frameMessageCount;
  1562. connection.log("Stopping forever frame.");
  1563. }
  1564. },
  1565. abort: function (connection, async) {
  1566. transportLogic.ajaxAbort(connection, async);
  1567. },
  1568. getConnection: function (id) {
  1569. return transportLogic.foreverFrame.connections[id];
  1570. },
  1571. started: function (connection) {
  1572. if (connection.onSuccess) {
  1573. connection.onSuccess();
  1574. connection.onSuccess = null;
  1575. delete connection.onSuccess;
  1576. } else if (changeState(connection,
  1577. signalR.connectionState.reconnecting,
  1578. signalR.connectionState.connected) === true) {
  1579. // If there's no onSuccess handler we assume this is a reconnect
  1580. $(connection).triggerHandler(events.onReconnect);
  1581. }
  1582. }
  1583. };
  1584. }(window.jQuery, window));
  1585. /* jquery.signalR.transports.longPolling.js */
  1586. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  1587. /*global window:false */
  1588. /// <reference path="jquery.signalR.transports.common.js" />
  1589. (function ($, window, undefined) {
  1590. "use strict";
  1591. var signalR = $.signalR,
  1592. events = $.signalR.events,
  1593. changeState = $.signalR.changeState,
  1594. isDisconnecting = $.signalR.isDisconnecting,
  1595. transportLogic = signalR.transports._logic;
  1596. signalR.transports.longPolling = {
  1597. name: "longPolling",
  1598. supportsKeepAlive: false,
  1599. reconnectDelay: 3000,
  1600. init: function (connection, onComplete) {
  1601. /// <summary>Pings the server to ensure availability</summary>
  1602. /// <param name="connection" type="signalr">Connection associated with the server ping</param>
  1603. /// <param name="onComplete" type="Function">Callback to call once initialization has completed</param>
  1604. var that = this,
  1605. pingLoop,
  1606. // pingFail is used to loop the re-ping behavior. When we fail we want to re-try.
  1607. pingFail = function (reason) {
  1608. if (isDisconnecting(connection) === false) {
  1609. connection.log("Server ping failed because '" + reason + "', re-trying ping.");
  1610. connection._.pingLoopId = window.setTimeout(pingLoop, that.reconnectDelay);
  1611. }
  1612. };
  1613. connection.log("Initializing long polling connection with server.");
  1614. pingLoop = function () {
  1615. // Ping the server, on successful ping call the onComplete method, otherwise if we fail call the pingFail
  1616. transportLogic.pingServer(connection).done(onComplete).fail(pingFail);
  1617. };
  1618. pingLoop();
  1619. },
  1620. start: function (connection, onSuccess, onFailed) {
  1621. /// <summary>Starts the long polling connection</summary>
  1622. /// <param name="connection" type="signalR">The SignalR connection to start</param>
  1623. var that = this,
  1624. fireConnect = function () {
  1625. tryFailConnect = fireConnect = $.noop;
  1626. connection.log("Longpolling connected.");
  1627. onSuccess();
  1628. // Reset onFailed to null because it shouldn't be called again
  1629. onFailed = null;
  1630. },
  1631. tryFailConnect = function () {
  1632. if (onFailed) {
  1633. onFailed();
  1634. onFailed = null;
  1635. connection.log("LongPolling failed to connect.");
  1636. return true;
  1637. }
  1638. return false;
  1639. },
  1640. privateData = connection._,
  1641. reconnectErrors = 0,
  1642. fireReconnected = function (instance) {
  1643. window.clearTimeout(privateData.reconnectTimeoutId);
  1644. privateData.reconnectTimeoutId = null;
  1645. if (changeState(connection,
  1646. signalR.connectionState.reconnecting,
  1647. signalR.connectionState.connected) === true) {
  1648. // Successfully reconnected!
  1649. connection.log("Raising the reconnect event.");
  1650. $(instance).triggerHandler(events.onReconnect);
  1651. }
  1652. },
  1653. // 1 hour
  1654. maxFireReconnectedTimeout = 3600000;
  1655. if (connection.pollXhr) {
  1656. connection.log("Polling xhr requests already exists, aborting.");
  1657. connection.stop();
  1658. }
  1659. privateData.reconnectTimeoutId = null;
  1660. privateData.pollTimeoutId = null;
  1661. // We start with an initialization procedure which pings the server to verify that it is there.
  1662. // On scucessful initialization we'll then proceed with starting the transport.
  1663. that.init(connection, function () {
  1664. connection.messageId = null;
  1665. privateData.pollTimeoutId = window.setTimeout(function () {
  1666. (function poll(instance, raiseReconnect) {
  1667. var messageId = instance.messageId,
  1668. connect = (messageId === null),
  1669. reconnecting = !connect,
  1670. polling = !raiseReconnect,
  1671. url = transportLogic.getUrl(instance, that.name, reconnecting, polling);
  1672. // If we've disconnected during the time we've tried to re-instantiate the poll then stop.
  1673. if (isDisconnecting(instance) === true) {
  1674. return;
  1675. }
  1676. connection.log("Opening long polling request to '" + url + "'.");
  1677. instance.pollXhr = $.ajax(
  1678. $.extend({}, $.signalR.ajaxDefaults, {
  1679. xhrFields: { withCredentials: connection.withCredentials },
  1680. url: url,
  1681. type: "GET",
  1682. dataType: connection.ajaxDataType,
  1683. contentType: connection.contentType,
  1684. success: function (result) {
  1685. var delay = 0,
  1686. minData,
  1687. data,
  1688. shouldReconnect;
  1689. connection.log("Long poll complete.");
  1690. // Reset our reconnect errors so if we transition into a reconnecting state again we trigger
  1691. // reconnected quickly
  1692. reconnectErrors = 0;
  1693. try {
  1694. minData = connection._parseResponse(result);
  1695. }
  1696. catch (error) {
  1697. transportLogic.handleParseFailure(instance, result, error.message, tryFailConnect);
  1698. return;
  1699. }
  1700. // If there's currently a timeout to trigger reconnect, fire it now before processing messages
  1701. if (privateData.reconnectTimeoutId !== null) {
  1702. fireReconnected();
  1703. }
  1704. fireConnect();
  1705. if (minData) {
  1706. data = transportLogic.maximizePersistentResponse(minData);
  1707. }
  1708. transportLogic.processMessages(instance, minData);
  1709. if (data &&
  1710. $.type(data.LongPollDelay) === "number") {
  1711. delay = data.LongPollDelay;
  1712. }
  1713. if (data && data.Disconnect) {
  1714. return;
  1715. }
  1716. if (isDisconnecting(instance) === true) {
  1717. return;
  1718. }
  1719. shouldReconnect = data && data.ShouldReconnect;
  1720. if (shouldReconnect) {
  1721. // Transition into the reconnecting state
  1722. transportLogic.ensureReconnectingState(instance);
  1723. }
  1724. // We never want to pass a raiseReconnect flag after a successful poll. This is handled via the error function
  1725. if (delay > 0) {
  1726. privateData.pollTimeoutId = window.setTimeout(function () {
  1727. poll(instance, shouldReconnect);
  1728. }, delay);
  1729. } else {
  1730. poll(instance, shouldReconnect);
  1731. }
  1732. },
  1733. error: function (data, textStatus) {
  1734. // Stop trying to trigger reconnect, connection is in an error state
  1735. // If we're not in the reconnect state this will noop
  1736. window.clearTimeout(privateData.reconnectTimeoutId);
  1737. privateData.reconnectTimeoutId = null;
  1738. if (textStatus === "abort") {
  1739. connection.log("Aborted xhr requst.");
  1740. return;
  1741. }
  1742. if (!tryFailConnect()) {
  1743. // Increment our reconnect errors, we assume all errors to be reconnect errors
  1744. // In the case that it's our first error this will cause Reconnect to be fired
  1745. // after 1 second due to reconnectErrors being = 1.
  1746. reconnectErrors++;
  1747. if (connection.state !== signalR.connectionState.reconnecting) {
  1748. connection.log("An error occurred using longPolling. Status = " + textStatus + ". Response = " + data.responseText + ".");
  1749. $(instance).triggerHandler(events.onError, [data.responseText]);
  1750. }
  1751. // We check the state here to verify that we're not in an invalid state prior to verifying Reconnect.
  1752. // If we're not in connected or reconnecting then the next ensureReconnectingState check will fail and will return.
  1753. // Therefore we don't want to change that failure code path.
  1754. if ((connection.state === signalR.connectionState.connected ||
  1755. connection.state === signalR.connectionState.reconnecting) &&
  1756. !transportLogic.verifyLastActive(connection)) {
  1757. return;
  1758. }
  1759. // Transition into the reconnecting state
  1760. // If this fails then that means that the user transitioned the connection into the disconnected or connecting state within the above error handler trigger.
  1761. if (!transportLogic.ensureReconnectingState(instance)) {
  1762. return;
  1763. }
  1764. privateData.pollTimeoutId = window.setTimeout(function () {
  1765. // If we've errored out we need to verify that the server is still there, so re-start initialization process
  1766. // This will ping the server until it successfully gets a response.
  1767. that.init(instance, function () {
  1768. // Call poll with the raiseReconnect flag as true
  1769. poll(instance, true);
  1770. });
  1771. }, that.reconnectDelay);
  1772. }
  1773. }
  1774. }));
  1775. // This will only ever pass after an error has occured via the poll ajax procedure.
  1776. if (reconnecting && raiseReconnect === true) {
  1777. // We wait to reconnect depending on how many times we've failed to reconnect.
  1778. // This is essentially a heuristic that will exponentially increase in wait time before
  1779. // triggering reconnected. This depends on the "error" handler of Poll to cancel this
  1780. // timeout if it triggers before the Reconnected event fires.
  1781. // The Math.min at the end is to ensure that the reconnect timeout does not overflow.
  1782. privateData.reconnectTimeoutId = window.setTimeout(function () { fireReconnected(instance); }, Math.min(1000 * (Math.pow(2, reconnectErrors) - 1), maxFireReconnectedTimeout));
  1783. }
  1784. }(connection));
  1785. // Set an arbitrary timeout to trigger onSuccess, this will alot for enough time on the server to wire up the connection.
  1786. // Will be fixed by #1189 and this code can be modified to not be a timeout
  1787. window.setTimeout(function () {
  1788. // Trigger the onSuccess() method because we've now instantiated a connection
  1789. fireConnect();
  1790. }, 250);
  1791. }, 250); // Have to delay initial poll so Chrome doesn't show loader spinner in tab
  1792. });
  1793. },
  1794. lostConnection: function (connection) {
  1795. throw new Error("Lost Connection not handled for LongPolling");
  1796. },
  1797. send: function (connection, data) {
  1798. transportLogic.ajaxSend(connection, data);
  1799. },
  1800. stop: function (connection) {
  1801. /// <summary>Stops the long polling connection</summary>
  1802. /// <param name="connection" type="signalR">The SignalR connection to stop</param>
  1803. window.clearTimeout(connection._.pollTimeoutId);
  1804. window.clearTimeout(connection._.reconnectTimeoutId);
  1805. delete connection._.pollTimeoutId;
  1806. delete connection._.reconnectTimeoutId;
  1807. if (connection.pollXhr) {
  1808. connection.pollXhr.abort();
  1809. connection.pollXhr = null;
  1810. delete connection.pollXhr;
  1811. }
  1812. },
  1813. abort: function (connection, async) {
  1814. transportLogic.ajaxAbort(connection, async);
  1815. }
  1816. };
  1817. }(window.jQuery, window));
  1818. /* jquery.signalR.hubs.js */
  1819. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  1820. /*global window:false */
  1821. /// <reference path="jquery.signalR.core.js" />
  1822. (function ($, window, undefined) {
  1823. "use strict";
  1824. // we use a global id for tracking callbacks so the server doesn't have to send extra info like hub name
  1825. var eventNamespace = ".hubProxy";
  1826. function makeEventName(event) {
  1827. return event + eventNamespace;
  1828. }
  1829. // Equivalent to Array.prototype.map
  1830. function map(arr, fun, thisp) {
  1831. var i,
  1832. length = arr.length,
  1833. result = [];
  1834. for (i = 0; i < length; i += 1) {
  1835. if (arr.hasOwnProperty(i)) {
  1836. result[i] = fun.call(thisp, arr[i], i, arr);
  1837. }
  1838. }
  1839. return result;
  1840. }
  1841. function getArgValue(a) {
  1842. return $.isFunction(a) ? null : ($.type(a) === "undefined" ? null : a);
  1843. }
  1844. function hasMembers(obj) {
  1845. for (var key in obj) {
  1846. // If we have any properties in our callback map then we have callbacks and can exit the loop via return
  1847. if (obj.hasOwnProperty(key)) {
  1848. return true;
  1849. }
  1850. }
  1851. return false;
  1852. }
  1853. function clearInvocationCallbacks(connection, error) {
  1854. /// <param name="connection" type="hubConnection" />
  1855. var callbacks = connection._.invocationCallbacks,
  1856. callback;
  1857. if (hasMembers(callbacks)) {
  1858. connection.log("Clearing hub invocation callbacks with error: " + error + ".");
  1859. }
  1860. // Reset the callback cache now as we have a local var referencing it
  1861. connection._.invocationCallbackId = 0;
  1862. delete connection._.invocationCallbacks;
  1863. connection._.invocationCallbacks = {};
  1864. // Loop over the callbacks and invoke them.
  1865. // We do this using a local var reference and *after* we've cleared the cache
  1866. // so that if a fail callback itself tries to invoke another method we don't
  1867. // end up with its callback in the list we're looping over.
  1868. for (var callbackId in callbacks) {
  1869. callback = callbacks[callbackId];
  1870. callback.method.call(callback.scope, { E: error });
  1871. }
  1872. }
  1873. // hubProxy
  1874. function hubProxy(hubConnection, hubName) {
  1875. /// <summary>
  1876. /// Creates a new proxy object for the given hub connection that can be used to invoke
  1877. /// methods on server hubs and handle client method invocation requests from the server.
  1878. /// </summary>
  1879. return new hubProxy.fn.init(hubConnection, hubName);
  1880. }
  1881. hubProxy.fn = hubProxy.prototype = {
  1882. init: function (connection, hubName) {
  1883. this.state = {};
  1884. this.connection = connection;
  1885. this.hubName = hubName;
  1886. this._ = {
  1887. callbackMap: {}
  1888. };
  1889. },
  1890. hasSubscriptions: function () {
  1891. return hasMembers(this._.callbackMap);
  1892. },
  1893. on: function (eventName, callback) {
  1894. /// <summary>Wires up a callback to be invoked when a invocation request is received from the server hub.</summary>
  1895. /// <param name="eventName" type="String">The name of the hub event to register the callback for.</param>
  1896. /// <param name="callback" type="Function">The callback to be invoked.</param>
  1897. var self = this,
  1898. callbackMap = self._.callbackMap;
  1899. // Normalize the event name to lowercase
  1900. eventName = eventName.toLowerCase();
  1901. // If there is not an event registered for this callback yet we want to create its event space in the callback map.
  1902. if (!callbackMap[eventName]) {
  1903. callbackMap[eventName] = {};
  1904. }
  1905. // Map the callback to our encompassed function
  1906. callbackMap[eventName][callback] = function (e, data) {
  1907. callback.apply(self, data);
  1908. };
  1909. $(self).bind(makeEventName(eventName), callbackMap[eventName][callback]);
  1910. return self;
  1911. },
  1912. off: function (eventName, callback) {
  1913. /// <summary>Removes the callback invocation request from the server hub for the given event name.</summary>
  1914. /// <param name="eventName" type="String">The name of the hub event to unregister the callback for.</param>
  1915. /// <param name="callback" type="Function">The callback to be invoked.</param>
  1916. var self = this,
  1917. callbackMap = self._.callbackMap,
  1918. callbackSpace;
  1919. // Normalize the event name to lowercase
  1920. eventName = eventName.toLowerCase();
  1921. callbackSpace = callbackMap[eventName];
  1922. // Verify that there is an event space to unbind
  1923. if (callbackSpace) {
  1924. // Only unbind if there's an event bound with eventName and a callback with the specified callback
  1925. if (callbackSpace[callback]) {
  1926. $(self).unbind(makeEventName(eventName), callbackSpace[callback]);
  1927. // Remove the callback from the callback map
  1928. delete callbackSpace[callback];
  1929. // Check if there are any members left on the event, if not we need to destroy it.
  1930. if (!hasMembers(callbackSpace)) {
  1931. delete callbackMap[eventName];
  1932. }
  1933. }
  1934. else if (!callback) { // Check if we're removing the whole event and we didn't error because of an invalid callback
  1935. $(self).unbind(makeEventName(eventName));
  1936. delete callbackMap[eventName];
  1937. }
  1938. }
  1939. return self;
  1940. },
  1941. invoke: function (methodName) {
  1942. /// <summary>Invokes a server hub method with the given arguments.</summary>
  1943. /// <param name="methodName" type="String">The name of the server hub method.</param>
  1944. var self = this,
  1945. connection = self.connection,
  1946. args = $.makeArray(arguments).slice(1),
  1947. argValues = map(args, getArgValue),
  1948. data = { H: self.hubName, M: methodName, A: argValues, I: connection._.invocationCallbackId },
  1949. d = $.Deferred(),
  1950. callback = function (minResult) {
  1951. var result = self._maximizeHubResponse(minResult);
  1952. // Update the hub state
  1953. $.extend(self.state, result.State);
  1954. if (result.Error) {
  1955. // Server hub method threw an exception, log it & reject the deferred
  1956. if (result.StackTrace) {
  1957. connection.log(result.Error + "\n" + result.StackTrace + ".");
  1958. }
  1959. d.rejectWith(self, [result.Error]);
  1960. } else {
  1961. // Server invocation succeeded, resolve the deferred
  1962. d.resolveWith(self, [result.Result]);
  1963. }
  1964. };
  1965. connection._.invocationCallbacks[connection._.invocationCallbackId.toString()] = { scope: self, method: callback };
  1966. connection._.invocationCallbackId += 1;
  1967. if (!$.isEmptyObject(self.state)) {
  1968. data.S = self.state;
  1969. }
  1970. connection.send(self.connection.json.stringify(data));
  1971. return d.promise();
  1972. },
  1973. _maximizeHubResponse: function (minHubResponse) {
  1974. return {
  1975. State: minHubResponse.S,
  1976. Result: minHubResponse.R,
  1977. Id: minHubResponse.I,
  1978. Error: minHubResponse.E,
  1979. StackTrace: minHubResponse.T
  1980. };
  1981. }
  1982. };
  1983. hubProxy.fn.init.prototype = hubProxy.fn;
  1984. // hubConnection
  1985. function hubConnection(url, options) {
  1986. /// <summary>Creates a new hub connection.</summary>
  1987. /// <param name="url" type="String">[Optional] The hub route url, defaults to "/signalr".</param>
  1988. /// <param name="options" type="Object">[Optional] Settings to use when creating the hubConnection.</param>
  1989. var settings = {
  1990. qs: null,
  1991. logging: false,
  1992. useDefaultPath: true
  1993. };
  1994. $.extend(settings, options);
  1995. if (!url || settings.useDefaultPath) {
  1996. url = (url || "") + "/signalr";
  1997. }
  1998. return new hubConnection.fn.init(url, settings);
  1999. }
  2000. hubConnection.fn = hubConnection.prototype = $.connection();
  2001. hubConnection.fn.init = function (url, options) {
  2002. var settings = {
  2003. qs: null,
  2004. logging: false,
  2005. useDefaultPath: true
  2006. },
  2007. connection = this;
  2008. $.extend(settings, options);
  2009. // Call the base constructor
  2010. $.signalR.fn.init.call(connection, url, settings.qs, settings.logging);
  2011. // Object to store hub proxies for this connection
  2012. connection.proxies = {};
  2013. connection._.invocationCallbackId = 0;
  2014. connection._.invocationCallbacks = {};
  2015. // Wire up the received handler
  2016. connection.received(function (minData) {
  2017. var data, proxy, dataCallbackId, callback, hubName, eventName;
  2018. if (!minData) {
  2019. return;
  2020. }
  2021. if (typeof (minData.I) !== "undefined") {
  2022. // We received the return value from a server method invocation, look up callback by id and call it
  2023. dataCallbackId = minData.I.toString();
  2024. callback = connection._.invocationCallbacks[dataCallbackId];
  2025. if (callback) {
  2026. // Delete the callback from the proxy
  2027. connection._.invocationCallbacks[dataCallbackId] = null;
  2028. delete connection._.invocationCallbacks[dataCallbackId];
  2029. // Invoke the callback
  2030. callback.method.call(callback.scope, minData);
  2031. }
  2032. } else {
  2033. data = this._maximizeClientHubInvocation(minData);
  2034. // We received a client invocation request, i.e. broadcast from server hub
  2035. connection.log("Triggering client hub event '" + data.Method + "' on hub '" + data.Hub + "'.");
  2036. // Normalize the names to lowercase
  2037. hubName = data.Hub.toLowerCase();
  2038. eventName = data.Method.toLowerCase();
  2039. // Trigger the local invocation event
  2040. proxy = this.proxies[hubName];
  2041. // Update the hub state
  2042. $.extend(proxy.state, data.State);
  2043. $(proxy).triggerHandler(makeEventName(eventName), [data.Args]);
  2044. }
  2045. });
  2046. connection.error(function (errData, origData) {
  2047. var callbackId, callback;
  2048. if (connection.transport && connection.transport.name === "webSockets") {
  2049. // WebSockets connections have all callbacks removed on reconnect instead
  2050. // as WebSockets sends are fire & forget
  2051. return;
  2052. }
  2053. if (!origData) {
  2054. // No original data passed so this is not a send error
  2055. return;
  2056. }
  2057. try {
  2058. origData = connection.json.parse(origData);
  2059. } catch (e) {
  2060. // The original data is not a JSON payload so this is not a send error
  2061. return;
  2062. }
  2063. callbackId = origData.I;
  2064. callback = connection._.invocationCallbacks[callbackId];
  2065. // Verify that there is a callback bound (could have been cleared)
  2066. if (callback) {
  2067. // Delete the callback
  2068. connection._.invocationCallbacks[callbackId] = null;
  2069. delete connection._.invocationCallbacks[callbackId];
  2070. // Invoke the callback with an error to reject the promise
  2071. callback.method.call(callback.scope, { E: errData });
  2072. }
  2073. });
  2074. connection.reconnecting(function () {
  2075. if (connection.transport && connection.transport.name === "webSockets") {
  2076. clearInvocationCallbacks(connection, "Connection started reconnecting before invocation result was received.");
  2077. }
  2078. });
  2079. connection.disconnected(function () {
  2080. clearInvocationCallbacks(connection, "Connection was disconnected before invocation result was received.");
  2081. });
  2082. };
  2083. hubConnection.fn._maximizeClientHubInvocation = function (minClientHubInvocation) {
  2084. return {
  2085. Hub: minClientHubInvocation.H,
  2086. Method: minClientHubInvocation.M,
  2087. Args: minClientHubInvocation.A,
  2088. State: minClientHubInvocation.S
  2089. };
  2090. };
  2091. hubConnection.fn._registerSubscribedHubs = function () {
  2092. /// <summary>
  2093. /// Sets the starting event to loop through the known hubs and register any new hubs
  2094. /// that have been added to the proxy.
  2095. /// </summary>
  2096. var connection = this;
  2097. if (!connection._subscribedToHubs) {
  2098. connection._subscribedToHubs = true;
  2099. connection.starting(function () {
  2100. // Set the connection's data object with all the hub proxies with active subscriptions.
  2101. // These proxies will receive notifications from the server.
  2102. var subscribedHubs = [];
  2103. $.each(connection.proxies, function (key) {
  2104. if (this.hasSubscriptions()) {
  2105. subscribedHubs.push({ name: key });
  2106. connection.log("Client subscribed to hub '" + key + "'.");
  2107. }
  2108. });
  2109. if (subscribedHubs.length === 0) {
  2110. connection.log("No hubs have been subscribed to. The client will not receive data from hubs. To fix, declare at least one client side function prior to connection start for each hub you wish to subscribe to.");
  2111. }
  2112. connection.data = connection.json.stringify(subscribedHubs);
  2113. });
  2114. }
  2115. };
  2116. hubConnection.fn.createHubProxy = function (hubName) {
  2117. /// <summary>
  2118. /// Creates a new proxy object for the given hub connection that can be used to invoke
  2119. /// methods on server hubs and handle client method invocation requests from the server.
  2120. /// </summary>
  2121. /// <param name="hubName" type="String">
  2122. /// The name of the hub on the server to create the proxy for.
  2123. /// </param>
  2124. // Normalize the name to lowercase
  2125. hubName = hubName.toLowerCase();
  2126. var proxy = this.proxies[hubName];
  2127. if (!proxy) {
  2128. proxy = hubProxy(this, hubName);
  2129. this.proxies[hubName] = proxy;
  2130. }
  2131. this._registerSubscribedHubs();
  2132. return proxy;
  2133. };
  2134. hubConnection.fn.init.prototype = hubConnection.fn;
  2135. $.hubConnection = hubConnection;
  2136. }(window.jQuery, window));
  2137. /* jquery.signalR.version.js */
  2138. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  2139. /*global window:false */
  2140. /// <reference path="jquery.signalR.core.js" />
  2141. (function ($, undefined) {
  2142. $.signalR.version = "1.2.2";
  2143. }(window.jQuery));