|
24 | 24 | onReconnect: "onReconnect",
|
25 | 25 | onDisconnect: "onDisconnect"
|
26 | 26 | },
|
27 |
| - log = function (msg) { |
| 27 | + log = function (msg, logging) { |
| 28 | + if (logging === false) { |
| 29 | + return; |
| 30 | + } |
28 | 31 | var m;
|
29 | 32 | if (typeof (window.console) === "undefined") {
|
30 | 33 | return;
|
|
37 | 40 | }
|
38 | 41 | };
|
39 | 42 |
|
40 |
| - signalR = function (url, qs) { |
| 43 | + signalR = function (url, qs, logging) { |
41 | 44 | /// <summary>Creates a new SignalR connection for the given url</summary>
|
42 | 45 | /// <param name="url" type="String">The URL of the long polling endpoint</param>
|
43 | 46 | /// <param name="qs" type="Object">
|
44 | 47 | /// [Optional] Custom querystring parameters to add to the connection URL.
|
45 | 48 | /// If an object, every non-function member will be added to the querystring.
|
46 | 49 | /// If a string, it's added to the QS as specified.
|
47 | 50 | /// </param>
|
| 51 | + /// <param name="logging" type="Boolean"> |
| 52 | + /// [Optional] A flag indicating whether connection logging is enabled to the browser |
| 53 | + /// console/log. Defaults to false. |
| 54 | + /// </param> |
48 | 55 | /// <returns type="signalR" />
|
49 | 56 |
|
50 |
| - return new signalR.fn.init(url, qs); |
| 57 | + return new signalR.fn.init(url, qs, logging); |
51 | 58 | };
|
52 | 59 |
|
53 | 60 | signalR.fn = signalR.prototype = {
|
54 |
| - init: function (url, qs) { |
| 61 | + init: function (url, qs, logging) { |
55 | 62 | this.url = url;
|
56 | 63 | this.qs = qs;
|
| 64 | + if (typeof (logging) === "boolean") { |
| 65 | + this.logging = logging; |
| 66 | + } |
57 | 67 | },
|
58 | 68 |
|
| 69 | + logging: false, |
| 70 | + |
59 | 71 | reconnectDelay: 2000,
|
60 | 72 |
|
61 | 73 | start: function (options, callback) {
|
|
353 | 365 |
|
354 | 366 | if (data) {
|
355 | 367 | if (data.Disconnect) {
|
356 |
| - log("disconnect command received from server"); |
| 368 | + log("Disconnect command received from server", connection.logging); |
357 | 369 |
|
358 | 370 | // Disconnected by the server
|
359 | 371 | connection.stop();
|
|
369 | 381 | $connection.trigger(events.onReceived, [this]);
|
370 | 382 | }
|
371 | 383 | catch (e) {
|
372 |
| - log("Error raising received " + e); |
| 384 | + log("Error raising received " + e, connection.logging); |
373 | 385 | $(connection).trigger(events.onError, [e]);
|
374 | 386 | }
|
375 | 387 | });
|
|
461 | 473 | $connection.trigger(events.onReceived, [this]);
|
462 | 474 | }
|
463 | 475 | catch (e) {
|
464 |
| - log("Error raising received " + e); |
| 476 | + log("Error raising received " + e, connection.logging); |
465 | 477 | }
|
466 | 478 | });
|
467 | 479 | } else {
|
|
512 | 524 | connection.eventSource = new window.EventSource(url);
|
513 | 525 | }
|
514 | 526 | catch (e) {
|
515 |
| - log("EventSource failed trying to connect with error " + e.Message); |
| 527 | + log("EventSource failed trying to connect with error " + e.Message, connection.logging); |
516 | 528 | if (onFailed) {
|
517 | 529 | // The connection failed, call the failed callback
|
518 | 530 | onFailed();
|
|
521 | 533 | $connection.trigger(events.onError, [e]);
|
522 | 534 | if (reconnecting) {
|
523 | 535 | // If we were reconnecting, rather than doing initial connect, then try reconnect again
|
524 |
| - log("EventSource reconnecting"); |
| 536 | + log("EventSource reconnecting", connection.logging); |
525 | 537 | that.reconnect(connection);
|
526 | 538 | }
|
527 | 539 | }
|
|
532 | 544 | // and raise on failed
|
533 | 545 | connectTimeOut = window.setTimeout(function () {
|
534 | 546 | if (opened === false) {
|
535 |
| - log("EventSource timed out trying to connect"); |
| 547 | + log("EventSource timed out trying to connect", connection.logging); |
536 | 548 |
|
537 | 549 | if (onFailed) {
|
538 | 550 | onFailed();
|
539 | 551 | }
|
540 | 552 |
|
541 | 553 | if (reconnecting) {
|
542 | 554 | // If we were reconnecting, rather than doing initial connect, then try reconnect again
|
543 |
| - log("EventSource reconnecting"); |
| 555 | + log("EventSource reconnecting", connection.logging); |
544 | 556 | that.reconnect(connection);
|
545 | 557 | } else {
|
546 | 558 | that.stop(connection);
|
|
550 | 562 | that.timeOut);
|
551 | 563 |
|
552 | 564 | connection.eventSource.addEventListener("open", function (e) {
|
553 |
| - log("EventSource connected"); |
| 565 | + log("EventSource connected", connection.logging); |
554 | 566 |
|
555 | 567 | if (connectTimeOut) {
|
556 | 568 | window.clearTimeout(connectTimeOut);
|
|
585 | 597 | return;
|
586 | 598 | }
|
587 | 599 |
|
588 |
| - log("EventSource readyState: " + connection.eventSource.readyState); |
| 600 | + log("EventSource readyState: " + connection.eventSource.readyState, connection.logging); |
589 | 601 |
|
590 | 602 | if (e.eventPhase === window.EventSource.CLOSED) {
|
591 | 603 | // connection closed
|
|
594 | 606 | // doesn't allow us to change the URL when reconnecting. We need
|
595 | 607 | // to change the URL to not include the /connect suffix, and pass
|
596 | 608 | // the last message id we received.
|
597 |
| - log("EventSource reconnecting due to the server connection ending"); |
| 609 | + log("EventSource reconnecting due to the server connection ending", connection.logging); |
598 | 610 | that.reconnect(connection);
|
599 | 611 | }
|
600 | 612 | else {
|
601 | 613 | // The EventSource has closed, either because its close() method was called,
|
602 | 614 | // or the server sent down a "don't reconnect" frame.
|
603 |
| - log("EventSource closed"); |
| 615 | + log("EventSource closed", connection.logging); |
604 | 616 | that.stop(connection);
|
605 | 617 | }
|
606 | 618 | } else {
|
607 | 619 | // connection error
|
608 |
| - log("EventSource error"); |
| 620 | + log("EventSource error", connection.logging); |
609 | 621 | $connection.trigger(events.onError);
|
610 | 622 | }
|
611 | 623 | }, false);
|
|
663 | 675 |
|
664 | 676 | frame.bind("readystatechange", function () {
|
665 | 677 | if ($.inArray(this.readyState, ["loaded", "complete"]) >= 0) {
|
666 |
| - log("Forever frame iframe readyState changed to " + this.readyState + ", reconnecting"); |
| 678 | + log("Forever frame iframe readyState changed to " + this.readyState + ", reconnecting", connection.logging); |
667 | 679 | that.reconnect(connection);
|
668 | 680 | }
|
669 | 681 | });
|
|
0 commit comments