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