SignalR - 为什么当用户重新加载页面时会调用另一个集线器的 Disconnect 方法?
背景:
我无法弄清楚为什么从尚未初始化的集线器调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它写在 signalR 项目的 wiki 中:
https://github.com/SignalR/SignalR/wiki/Hubs
It's written in signalR project's wiki:
https://github.com/SignalR/SignalR/wiki/Hubs