abp.signalr-client.js 3.7 KB

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