abp.signalr-client.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var abp = abp || {};
  2. (function () {
  3. // Check if SignalR is defined
  4. if (!signalR) {
  5. return;
  6. }
  7. // Create namespaces
  8. abp.signalr = abp.signalr || {};
  9. abp.signalr.hubs = abp.signalr.hubs || {};
  10. // Configure the connection for abp.signalr.hubs.common
  11. function configureConnection(connection) {
  12. // Set the common hub
  13. abp.signalr.hubs.common = connection;
  14. // Reconnect loop
  15. function start() {
  16. connection.start().catch(function () {
  17. setTimeout(function () {
  18. start();
  19. }, 5000);
  20. });
  21. }
  22. // Reconnect if hub disconnects
  23. connection.onclose(function (e) {
  24. if (e) {
  25. abp.log.debug('Connection closed with error: ' + e);
  26. } else {
  27. abp.log.debug('Disconnected');
  28. }
  29. if (!abp.signalr.autoReconnect) {
  30. return;
  31. }
  32. start();
  33. });
  34. // Register to get notifications
  35. connection.on('getNotification', function (notification) {
  36. abp.event.trigger('abp.notifications.received', notification);
  37. });
  38. }
  39. // Connect to the server for abp.signalr.hubs.common
  40. function connect() {
  41. var url = abp.signalr.url || (abp.appPath + 'signalr');
  42. // Start the connection
  43. startConnection(url, configureConnection)
  44. .then(function (connection) {
  45. abp.log.debug('Connected to SignalR server!'); //TODO: Remove log
  46. abp.event.trigger('abp.signalr.connected');
  47. // Call the Register method on the hub
  48. connection.invoke('register').then(function () {
  49. abp.log.debug('Registered to the SignalR server!'); //TODO: Remove log
  50. });
  51. })
  52. .catch(function (error) {
  53. abp.log.debug(error.message);
  54. });
  55. }
  56. // Starts a connection with transport fallback - if the connection cannot be started using
  57. // the webSockets transport the function will fallback to the serverSentEvents transport and
  58. // if this does not work it will try longPolling. If the connection cannot be started using
  59. // any of the available transports the function will return a rejected Promise.
  60. function startConnection(url, configureConnection) {
  61. if (abp.signalr.remoteServiceBaseUrl) {
  62. url = abp.signalr.remoteServiceBaseUrl + url;
  63. }
  64. // Add query string: https://github.com/aspnet/SignalR/issues/680
  65. if (abp.signalr.qs) {
  66. url += (url.indexOf('?') == -1 ? '?' : '&') + abp.signalr.qs;
  67. }
  68. return function start(transport) {
  69. abp.log.debug('Starting connection using ' + signalR.HttpTransportType[transport] + ' transport');
  70. var connection = new signalR.HubConnectionBuilder()
  71. .withUrl(url, transport)
  72. .build();
  73. if (configureConnection && typeof configureConnection === 'function') {
  74. configureConnection(connection);
  75. }
  76. return connection.start()
  77. .then(function () {
  78. return connection;
  79. })
  80. .catch(function (error) {
  81. abp.log.debug('Cannot start the connection using ' + signalR.HttpTransportType[transport] + ' transport. ' + error.message);
  82. if (transport !== signalR.HttpTransportType.LongPolling) {
  83. return start(transport + 1);
  84. }
  85. return Promise.reject(error);
  86. });
  87. }(signalR.HttpTransportType.WebSockets);
  88. }
  89. abp.signalr.autoConnect = abp.signalr.autoConnect === undefined ? true : abp.signalr.autoConnect;
  90. abp.signalr.autoReconnect = abp.signalr.autoReconnect === undefined ? true : abp.signalr.autoReconnect;
  91. abp.signalr.connect = abp.signalr.connect || connect;
  92. abp.signalr.startConnection = abp.signalr.startConnection || startConnection;
  93. if (abp.signalr.autoConnect && !abp.signalr.hubs.common) {
  94. abp.signalr.connect();
  95. }
  96. })();