abp.signalr.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var abp = abp || {};
  2. (function ($) {
  3. //Check if SignalR is defined
  4. if (!$ || !$.connection) {
  5. return;
  6. }
  7. //Create namespaces
  8. abp.signalr = abp.signalr || {};
  9. abp.signalr.hubs = abp.signalr.hubs || {};
  10. //Get the common hub
  11. abp.signalr.hubs.common = $.connection.abpCommonHub;
  12. var commonHub = abp.signalr.hubs.common;
  13. if (!commonHub) {
  14. return;
  15. }
  16. //Register to get notifications
  17. commonHub.client.getNotification = function (notification) {
  18. abp.event.trigger('abp.notifications.received', notification);
  19. };
  20. //Connect to the server
  21. abp.signalr.connect = function () {
  22. $.connection.hub.start().done(function () {
  23. abp.log.debug('Connected to SignalR server!'); //TODO: Remove log
  24. abp.event.trigger('abp.signalr.connected');
  25. commonHub.server.register().done(function () {
  26. abp.log.debug('Registered to the SignalR server!'); //TODO: Remove log
  27. });
  28. });
  29. };
  30. if (abp.signalr.autoConnect === undefined) {
  31. abp.signalr.autoConnect = true;
  32. }
  33. if (abp.signalr.autoConnect) {
  34. abp.signalr.connect();
  35. }
  36. //reconnect if hub disconnects
  37. $.connection.hub.disconnected(function () {
  38. if (!abp.signalr.autoConnect) {
  39. return;
  40. }
  41. setTimeout(function () {
  42. if ($.connection.hub.state === $.signalR.connectionState.disconnected) {
  43. $.connection.hub.start();
  44. }
  45. }, 5000);
  46. });
  47. })(jQuery);