问题描述
我有一个MVC项目,与多个页面。
I have an MVC project, with multiple pages.
我有一个长时间运行的进程,这将更新客户端上的进度。但是,此更新将只发送给一个客户端,而不是广播到所有。
I have a long running process, which updates the client on its progress. However, this update is sent only to a single client, instead of broadcasting to all.
Clients.Client(ConnectionId).sendMessage(msg);
在我layout.cshtml,这就是我如何连接到集线器
In my layout.cshtml, this is how I connect to the hub
var serverHub = $.connection.notifier;
window.hubReady = $.connection.hub.start(function () { });
问题是,当我浏览到另一个网页,我不再signalr收到的消息,因为连接ID已更改。
The problem is, when I navigate to another page, I no longer receive the messages from signalr, because the connection id has changed.
我应该如何解决这个问题,这样我signalr中心仍然可以将邮件发送到一个单一的客户端,而客户端从一个页面导航到页面。
How should I workaround this issue, such that my signalr hub can still send messages to a single client, while the client navigates from page to page.
推荐答案
您将要创建用户的服务器映射到连接ID的。参见:SignalR 1.0测试连接工厂。
You will want to create a server mapping of users to connection id's. See: SignalR 1.0 beta connection factory.
您想要让用户坚持过去的OnDisconnected事件,当他们用不同的连接ID连接可以继续抽数据到他们。
You will want to let your users persist past an OnDisconnected event and when they connect with a different connection Id you can continue pumping data down to them.
所以,思维过程可能如下:
So the thought process could be as follows:
- 页面负载,SignalR连接实例
- 一旦连接完全启动呼叫=> TryCreateUser(返回一个用户,无论存在还是它的创建)。
- 长时间运行的进程启动
- 客户端页面的变化 - > SignalR连接停止
- 新页面加载,新SignalR的连接实例
- 一旦连接完全启动看看你是否有一个cookie,会话,或某些类型的数据重新presenting你=> TryCreateUser(用户数据)谁(返回的最后一页上创建的用户)。
- 数据继续抽空给用户。
注意:如果您需要身份验证方法,你将不得不开始之前的连接进行身份验证和SignalR连接的生命周期中的数据不能改变,那就只能创建/修改,而连接处于断开状态。
Note: if you take an authentication approach you will have to be authenticated prior to starting a connection and that data cannot change during the lifetime of a SignalR connection, it can only be created/modified while the connection is in the disconnected state.
这篇关于在重用同signalR连接,同时通过不同的页面导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!