SignalR和浏览器连接限制 [英] SignalR and Browser Connection limit

查看:2712
本文介绍了SignalR和浏览器连接限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的应用程序与SignalR进行测试。当页面加载它调用服务器上的一个函数,该函数然后调用打印屏幕上的一个消息的客户端功能。我这样做,检查客户端和服务器功能工程和SignalR通信工作正常。

I made a simple app with SignalR for testing. When the page loads it calls a function on the server, that function then calls a client function that prints a message on the screen. I did that to check that both the client and server function works and SignalR communication works ok.

我的问题是,如果我打开两个不同的标签相同的页面(在Chrome做到了),第一个页面加载不错,但第二页不会调用服务器的功能 - 只有当我关闭的第一页。

My problem is that if I open the same page on two different tabs (did it in Chrome), the first page loads ok, but the second page doesn't call the server's functions - ONLY if I close the first page.

因此​​,据我明白,他们很可能是相关的,不允许SignalR连接更多然后一次(实际上是两个,一个用于接收,一个用于发送)的浏览器的连接限制

So as far as I understand, their is probably a connection limitation that is related to the browser that doesn't allow SignalR to connect more then once (actually two, one for receiving and one for sending)

更新:我已经找我们,其他的标签开放的地方,但现在我已经通过检查,它允许只有4个标签页 /页面与连接活跃。如果我试图把同一个页面一个新的标签被发送任何数据,当我关闭其他标签之一上,新标签发送数据的时候了。

Update: I've find our that other tabs where open, but now I've checked it through and it allows only 4 tabs / pages to be active with connections. If I try to put the same page on a new tab no data is being sent, when I close one of the other tabs, the new tab sends the data right away.

我想,要知道是否有针对任何解决方案,因为我想这个连接可用,如果用户决定要打开两个标签或更多相同的页面内容。

What I wanted to know if there is any solution for that, because I want this connectivity to be available if the user decide to open the same page on two tabs or more.

我不认为这有什么与IIS,因为我知道它可以接受的连接十万。

I don't believe that it has anything to do with IIS, because from what I know it can accept thousands of connections.

推荐答案

这问题将由未来最好的解决<一个href=\"http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#channel-messaging\">Channel消息规范,没有被任何浏览器的最新实施的,但我设法通过的限制由亚历克斯·福特描述和使用连接的localStorage 作为选项卡之间的消息总线。

This problem would be best addressed by the future Channel Messaging specification, which has not been implemented by any browsers to-date, but I managed to solve it by limiting the number of connections as described by Alex Ford and using localStorage as a message bus between tabs.

存储事件让你传播的选项卡之间的数据,同时保持一个SignalR连接打开(从而preventing连接饱和度)。调用 localStorage.setItem('sharedKey',sharedData)将提高存储事件中的所有其他选项卡(不来电):

The storage event lets you propagate data between tabs while keeping a single SignalR connection open (thereby preventing connection saturation). Calling localStorage.setItem('sharedKey', sharedData) will raise the storage event in all other tabs (not the caller):

$(window).bind('storage', function (e) {
    var sharedData = localStorage.getItem('sharedKey');
    if (sharedData !== null)
        console.log(
            'A tab called localStorage.setItem("sharedData",'+sharedData+')'
        );
});

您可以测试如果($ .connection.hub.state === 1)来确定给定的标签应通过的localStorage通知其他选项卡(亚历克斯礼貌),以prevent复制 localStorage.setItem 来电。

You can test if ($.connection.hub.state === 1) to determine if a given tab should notify the other tabs via localStorage (courtesy of Alex) to prevent duplicate localStorage.setItem calls.

Facebook上克服了服务在几个子域持久连接这个浏览器限制,但是这可能复杂化的部署和测试。

Facebook overcomes this browser limitation by serving persistent connections over several sub-domains, but this can complicate deployment and testing.

旧的连接:在Alex的解决方案,你必须要小心断开连接()不会被调用(如除外),而你填你的 HubConnections 桶(或资料库)与旧轮毂连接。如果会话ID不会改变(可能发生的),这可能会建立,即使没有一个是活跃SignalR连接prevent新客户。另外,时间戳新的连接,并有一个滑动过期,以尽量减少可能产生的影响。

Old Connections: In Alex's solution, you need to be careful of Disconnect() not being called (e.g. exception), and you filling up your HubConnections bucket (or repository) with old hub connections. If the Session ID does not change (can happen), this may prevent new clients from establishing a SignalR connection even though none are active. Alternatively, timestamp new connections and have a sliding expiration to minimise potential impact.

锁定: 的localStorage ,因为它不执行任何锁定为的这里描述。

Locking: localStorage may be subject to race conditions as it does not implement any locking as described here.

要支持不同类型的事件,你应该带code时的 EVENTTYPE 的在你的JSON消息并测试它放在存储事件。

To support different types of events, you should encode an eventType in your JSON messages and test for it on the storage event.

如果一个SignalR连接不能建立,我回落到轮询服务器每45秒来检索通知计数

If a SignalR connection cannot be established, I fall back onto polling the server every 45 seconds to retrieve a notification count.

如果你不希望使用本地存储,您可以使用Cookie,但它不是干净。

If you don't want to use localStorage, you can use cookies, but it's not as clean.

这篇关于SignalR和浏览器连接限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆