- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Durandal 编写一个单页应用程序,并计划使用 SignalR 来实现某些功能。首先,我有一个顶部栏,用于监听服务器可能发送的通知。该站点启动与“TopBarNotificationHub”的连接。
在其中一个页面上,我想连接到另一个中心,因为两个用户可能会同时编辑此页面上的数据,并且我想通知是否有人更新了数据。没问题,这工作正常。
但是,当离开该页面时,我只想断开与第二个集线器的连接,但我找不到实现此目的的方法。如果我只是说 hub.connection.stop();与 eTopBarNotificationHub 的连接也会停止(因为它是共享的)。
有没有一种方法可以只留下一个集线器代理并让另一个集线器存在?
由于这是一个 SPA,“shell”永远不会重新加载,因此它不会再次连接到集线器...我也许可以在每次另一个页面与集线器断开连接时强制它重新连接,但可能会有一个更清洁的解决方案...
提前致谢。
//J
最佳答案
如果您在单个页面上使用多个中心,那没问题,但它们共享相同的连接,因此除了接收更新之外,它不会占用客户端上的更多资源。
因此,要“连接到集线器或从集线器断开连接”,您需要稍微重新架构。如果您在服务器端使用组而不是客户端,则可以通过调用(例如)Hub1.Register
方法并将客户端粘贴到该方法中的相关组中,从而向集线器“注册”。要“取消注册”,您可以调用(例如)Hub1.DeRegister
并从该方法中的组中删除客户端的 ConnectionId
。然后,当您向客户端推送更新时,您可以只使用 Group 而不是 Clients.All
。
(假定服务器语言为 C#,因为您未在标记中指定)
Groups.Add(Context.ConnectionId, groupNameForHub);
Groups.Remove(Context.ConnectionId, id.ToString());
Clients.Group(groupNameForHub).clientMethodName(param1, param2);
不过,为了避免混淆,请注意 Hub1 中名为“myGroup”的组与 Hub2 中名为“myGroup”的组是分开的。
这正是推荐的方法 in the documents (复制到这里以防它们在以后的版本中移动/更改):
Multiple Hubs
You can define multiple Hub classes in an application. When you do that, the connection is shared but groups are separate:
• All clients will use the same URL to establish a SignalR connection with your service ("/signalr" or your custom URL if you specified one), and that connection is used for all Hubs defined by the service.
There is no performance difference for multiple Hubs compared to defining all Hub functionality in a single class.
• All Hubs get the same HTTP request information.
Since all Hubs share the same connection, the only HTTP request information that the server gets is what comes in the original HTTP request that establishes the SignalR connection. If you use the connection request to pass information from the client to the server by specifying a query string, you can't provide different query strings to different Hubs. All Hubs will receive the same information.
• The generated JavaScript proxies file will contain proxies for all Hubs in one file.
For information about JavaScript proxies, see SignalR Hubs API Guide - JavaScript Client - The generated proxy and what it does for you.
• Groups are defined within Hubs.
In SignalR you can define named groups to broadcast to subsets of connected clients. Groups are maintained separately for each Hub. For example, a group named "Administrators" would include one set of clients for your ContosoChatHub class, and the same group name would refer to a different set of clients for your StockTickerHub class.
关于signalr - 连接到多个集线器并仅与一个集线器断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737678/
我想知道如何存储在调用时可以触发到连接的客户端的变量服务器端? 我使用并发字典来跟踪连接 GUID 并想知道我是否可以在服务器上存储字符串或其他数据类型,例如最后 10 条消息,因此当出现新连接时,客
无法让 SignalR 在我的机器上工作(使用 IE9)。在输入一些文本并单击提交时,文本未按预期列出。另外,我希望从多个浏览器实例更新列表,但它不会发生。没有错误。有人可以帮忙吗? C# names
找到后this update围绕与组的通信,似乎一个组与集线器名称无关。如果这是正确的(如果我弄错了,请告诉我)集线器有什么方法可以访问另一个集线器的组,或者更好地拥有某种全局组? 我的问题是我有一个
抽象的 嗨,我在考虑是否可以使用 SignalR 发送消息。假设客户端断开连接,但最终在很短的时间内(例如 3 秒)重新连接。客户端会在断开连接时收到所有发送给他的消息吗? 例如,让我们考虑 Long
我正在为中等负载的 Web 应用程序评估 SignalR。 我们期望大约 500 条消息/秒,这对 SignalR 来说应该不是问题。 但是,我们担心此解决方案的可靠性。我们的环境有一个有问题的网络,
我有以下JS工作: var chat = $.connection.appHub; 我的应用程序有一个单一的中心AppHub,它处理两种类型的通知-Chat和Other。我使用一个集线器,因为我需要一
我升级到.NET 4.5,现在SignalR似乎坚持要在Firefox / Chrome中使用WebSockets-即使我只是在没有WebSocket Server的Windows 7上使用。 该请求
您能否在服务器端辨别给定的SignalR连接使用哪种传输方法? (WebSocket,SSE,长时间轮询等?) 最佳答案 在集线器内部,您可以通过查看请求的查询字符串来检测正在使用的传输: Conte
我在MVC4应用程序中使用Visual Studio 2012 Ultimate RC,SignalR 0.5.1和Jquery 1.7.2。 我看过:MVC4 SignalR "signalr/hu
假设我的网络连接中断了几秒钟,并且我错过了一些 SignalR 服务器推送的消息。 当我重新获得网络连接时,我错过的消息是否会丢失?或者当我重新连接时 signalR 会处理它们并将它们推出吗? 如果
如果我有一个 signalR 集线器,它向所有具有相当大的有效负载或私有(private)信息的客户端发送消息,那么所有连接到该集线器的客户端(同一组的一部分)是否都会收到消息,即使它们没有订阅客户端
如果我有一个 signalR 集线器,它向所有具有相当大的有效负载或私有(private)信息的客户端发送消息,那么所有连接到该集线器的客户端(同一组的一部分)是否都会收到消息,即使它们没有订阅客户端
谁能让我知道 SignalR 内部是如何以高级方式工作的? 我猜测它正在使用 Response.Flush 刷新数据,并且在客户端以一定的时间间隔发送 Ajax 请求。正确吗? 最佳答案 不,Sign
我正在使用 Microsoft SignalR 向浏览器推送通知。这些通知是由其他浏览器的操作触发的。我想做一个有时发送通知的后台任务。例如,在 12:45:21,我想向所有连接的用户发出通知,即使他
我的 SignalR 存在以下问题,用户从移动设备访问我的网站。 问题在于,当手机锁定或用户转到另一个应用程序时,会在服务器上触发 Disconnect() 方法(我尝试向服务器发送消息并等待它通过
如果我使用 SignalR 发送消息,客户端是否可能收不到消息?您如何验证通信中是否出现任何错误?我想在发送服务器通知后将消息发送回服务器,但有没有更好的方法? 最佳答案 是的,客户端可能没有收到消息
Web 服务器如何使用 SignalR 同步调用客户端上的方法?这里的关键部分是服务器应该等待客户端将数据发送回服务器才能继续? 基本上,我希望将客户端方法调用视为 WCF 服务请求。调用客户端并等待
我正在尝试调试 SignalR 连接问题,虽然我知道如何启用客户端日志记录,但我不知道如何启用服务器端日志记录。它似乎没有将任何内容记录到控制台。 我试过在 HubConfiguration 中使用
如果 SignalR 服务器的内部缓冲区溢出,则它似乎可以在将消息发送到客户端之前丢弃消息(请参阅 https://github.com/SignalR/SignalR/issues/2075 )。目
我在 Blazor 服务器端应用程序中使用 SignalR。我将 Microsoft.AspNetCore.SignalR.Client Nuget 包 (v5.0.11) 添加到项目中,并使用以下代
我是一名优秀的程序员,十分优秀!