(tableId, group)
| 1 | function startSignalR(tableId, group) { |
| 2 | // Declare a proxy to reference the hub. |
| 3 | var notificationHub = $.connection.notificationHub; |
| 4 | $.connection.hub.url = "/signalr"; |
| 5 | // Create a function that the hub can call to broadcast messages. |
| 6 | notificationHub.client.updateItem = function (item) { |
| 7 | // Add the message to the page. |
| 8 | let tr = $('#' + tableId + item.Id); |
| 9 | if (tr) { |
| 10 | tr.children().remove(); |
| 11 | appendCells(tr, item); |
| 12 | } |
| 13 | }; |
| 14 | // Start the connection. |
| 15 | var connection = $.connection.hub; |
| 16 | |
| 17 | connection.disconnected(function () { |
| 18 | reconnectOnFail("Disconnected."); |
| 19 | }); |
| 20 | |
| 21 | startConnectionWithRetry(connection); |
| 22 | return notificationHub; |
| 23 | |
| 24 | function startConnectionWithRetry(hubConnection) { |
| 25 | hubConnection.start() |
| 26 | .done(async function () { |
| 27 | let items = await notificationHub.server.listenGroup(group); |
| 28 | $(`#${tableId} tbody tr`).remove(); |
| 29 | items.forEach(item => { |
| 30 | let tr = $(`<tr id="${tableId}${item.Id}" />`); |
| 31 | appendCells(tr, item); |
| 32 | $(`#${tableId} tbody`).append(tr); |
| 33 | }); |
| 34 | $('#status').html(`<h5 class='success'>Connected</h5>`); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | function reconnectOnFail(message) { |
| 39 | $('#status').html(`<h5 class='error'>${message} Reconnecting...</h5>`); |
| 40 | setTimeout(function () { |
| 41 | startConnectionWithRetry(connection) |
| 42 | }, 1000); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | function appendRow(tableId, item) { |
| 47 | let tr = $(`<tr id="${tableId}${item.Id}" />`); |
nothing calls this directly
no test coverage detected