SignalR 2 - Pending AJX requests after opening mul

2019-08-09 18:54发布

问题:

I'm using SignalR 2 and I'm having problems when I open multiple tabs of the same page. After opening 4 or 5 tabs, all the requests get in pending status, like if I had exceeded the maximum allowed by the browser. If I close a few tabs, everything works again. Even with all the tabs opened, if I open a different browser it works. This happens both in Chrome and Firefox. If I disable SignalR, I can open as many tabs as I want.

This is my code:

// Reference the auto-generated proxy for the hub.  
    notificator.hub = $.connection.messageHub;
    // Create a function that the hub can call back to display messages.
    notificator.hub.client.refreshNotifications = function () {
              // business code

    };

    $.connection.hub.start().done(function () {
        notificator.hubStarted = true;
    });
    $.connection.hub.disconnected(function () {
        notificator.hubStarted = false;
        setTimeout(function () {
            $.connection.hub.start();
        }, 2000); // Restart connection after 2 seconds.
    });

If I remove the handler for disconnected event, the problem persists. The notification system works correctly so SignalR is doing its job but it's causing me issues in the app. It's even slower.

回答1:

I found two solutions:

1) In my case, it's an intranet web application so I know that everybody supports WebSockets and WebSockets doesn't have the connection limitation. But, why this was not working? well, first I added logging to SignalR by adding this line:

$.connection.hub.logging = true;

Then I tried to force using WebSockets:

$.connection.hub.start({ transport: 'webSockets' }).done(function () { .. });

But, checking the logs I found out that WebSockets were not supported because they were not installed in the server. So, I followed these steps in Windows 2012 and installed it:

  1. Open Server Manager.
  2. Under the Manage menu, click Add Roles and Features.
  3. Select Role-based or Feature-based Installation, and then click Next.
  4. Select the appropriate server, (your local server is selected by default), and then click Next.
  5. Expand Web Server (IIS) in the Roles tree, then expand Web Server, and then expand Application Development.
  6. Select WebSocket Protocol, and then click Next.
  7. Click Install.

Source: http://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-websocket-protocol-support

After that, everything worked. In Chrome, under Network tab, you'll find a WS filter. Click on it and you should see the websocket.

2) Using IWC-SignalR:

https://github.com/slimjack/IWC-SignalR

It works this way:

One of the windows becomes a connection owner (choosen randomly) and holds the real SignalR connection. If connection owner is closed or crashed another window becomes a connection owner - this happens automatically. Inter-window communication is done by means of IWC.



标签: ajax signalr