SignalR - 为什么当用户重新加载页面时会调用另一个集线器的 Disconnect 方法?

发布于 2025-01-08 05:40:40 字数 745 浏览 1 评论 0原文

背景

我无法弄清楚为什么从尚未初始化的集线器调用 Disconnect。

我有两个不同的 MVC 视图,它们创建不同的信号连接:

  • ListView(JS 创建到 ListHub 的连接,它实现 IDisconnect)
  • DetailsView(JS 创建到 DetailsHub 的连接,它执行)实现 IDisconnect)

当用户登录时,他/她会被重定向到 ListView,其中 JavaScript 创建到 ListHub 的连接,以便页面了解锁定/解锁列表中项目的事件。当用户点击某个项目时,会向 ListHub 发送一个信号,并且该项目被用户锁定。之后,我使用 event.preventDefault() 阻止点击执行 GET 请求,效果很好。

问题是... 一旦用户离开到另一个页面 - 或者如果页面只是重新加载 - DetailsHub 的 Disconnect 方法就会被触发。我不明白为什么会发生这种情况。请记住,ListHub 是迄今为止唯一活跃的集线器。没有其他集线器或连接已初始化。尽管如此,这就是我得到的行为。

我想要实现的目标

我在这里想要实现的目标是在 DetailsHub 上引发/调用 Disconnect 时执行某些操作。我希望当用户与 ListHub 断开连接时什么也不会发生。

Background:

I'm having trouble figuring out why Disconnect is called from a hub that's not yet been initialized.

I have two different MVC views that creates different signal connections:

  • ListView (JS creates a connection to ListHub which does not implement IDisconnect)
  • DetailsView (JS creates a connection to DetailsHub which does implement IDisconnect)

When a user logs on, he/she is redirected to the ListView where a javascript creates a connection to the ListHub so that the page is aware of events that locks/unlocks items in the list. When the user clicks on an item, a signal is sent to the ListHub and the item is locked by the user. After that, I stop the click from doing the GET request by using event.preventDefault() and it works well.

The problem is...
Once the user leaves for another page - or if the page simply is reloaded - the Disconnect method of the DetailsHub is triggered. I don't get why this is happening. Remember, the ListHub is the only active hub thus far. No other Hubs or connections have been initialized. Still, this is the behavior I'm getting.

What I'm trying to achieve

What I'm trying to achieve here is to do something when Disconnect is raised/called on the DetailsHub. I want nothing to happen when the user disconnects from the ListHub.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2025-01-15 05:40:40

它写在 signalR 项目的 wiki 中:

每当客户端断开连接时,将在所有实现 IDisconnect 的集线器上调用 Disconnect 方法。调用此方法时,您可以使用 Context.ConnectionId 来访问断开连接的客户端。

https://github.com/SignalR/SignalR/wiki/Hubs

It's written in signalR project's wiki:

Whenever a client disconnects, the Disconnect method will be invoked on all hubs that implement IDisconnect. When this method is called you can use Context.ConnectionId to access the client that disconnected.

https://github.com/SignalR/SignalR/wiki/Hubs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文