在浏览不同页面时重复使用signalR中的相同连接

时间:2022-07-04 18:54:52

I have an MVC project, with multiple pages.

我有一个MVC项目,有多个页面。

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);

In my layout.cshtml, this is how I connect to the hub

在我的layout.cshtml中,这是我连接到集线器的方式

    var serverHub = $.connection.notifier;
    window.hubReady = $.connection.hub.start(function () { });

The problem is, when I navigate to another page, I no longer receive the messages from signalr, because the connection id has changed.

问题是,当我导航到另一个页面时,我不再收到来自signalr的消息,因为连接ID已更改。

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.

我应该如何解决此问题,以便我的信号器中心仍然可以向单个客户端发送消息,同时客户端从一个页面导航到另一个页面。

1 个解决方案

#1


5  

You will want to create a server mapping of users to connection id's. See: SignalR 1.0 beta connection factory.

您将需要创建用户到连接ID的服务器映射。请参阅:SignalR 1.0 beta连接工厂。

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.

您将希望让您的用户坚持通过OnDisconnected事件,当他们使用不同的连接ID连接时,您可以继续向他们提取数据。

So the thought process could be as follows:

所以思考过程可以如下:

  1. Page loads, SignalR connection is instantiated
  2. 页面加载,SignalR连接被实例化
  3. Once connection is fully started call => TryCreateUser (returns a user, whether it existed or it was created).
  4. 连接完全启动后,调用=> TryCreateUser(返回一个用户,无论它是否存在或是否已创建)。
  5. Long Running process Starts
  6. 长时间运行过程开始
  7. Client changes pages -> SignalR connection stops
  8. 客户端更改页面 - > SignalR连接停止
  9. New Page loads, new SignalR connection is instantiated
  10. 新页面加载,新的SignalR连接被实例化
  11. Once connection is fully started see if you have a cookie, session, or some type of data representing who you are => TryCreateUser(userData) (returns the user you created on the last page).
  12. 连接完全启动后,查看您是否有cookie,会话或某种类型的数据表示您是谁=> TryCreateUser(userData)(返回您在最后一页上创建的用户)。
  13. Data continues pumping down to user.
  14. 数据继续向用户灌输。

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连接的生命周期内数据不能更改,只能在连接处于断开连接状态时创建/修改数据。

#1


5  

You will want to create a server mapping of users to connection id's. See: SignalR 1.0 beta connection factory.

您将需要创建用户到连接ID的服务器映射。请参阅:SignalR 1.0 beta连接工厂。

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.

您将希望让您的用户坚持通过OnDisconnected事件,当他们使用不同的连接ID连接时,您可以继续向他们提取数据。

So the thought process could be as follows:

所以思考过程可以如下:

  1. Page loads, SignalR connection is instantiated
  2. 页面加载,SignalR连接被实例化
  3. Once connection is fully started call => TryCreateUser (returns a user, whether it existed or it was created).
  4. 连接完全启动后,调用=> TryCreateUser(返回一个用户,无论它是否存在或是否已创建)。
  5. Long Running process Starts
  6. 长时间运行过程开始
  7. Client changes pages -> SignalR connection stops
  8. 客户端更改页面 - > SignalR连接停止
  9. New Page loads, new SignalR connection is instantiated
  10. 新页面加载,新的SignalR连接被实例化
  11. Once connection is fully started see if you have a cookie, session, or some type of data representing who you are => TryCreateUser(userData) (returns the user you created on the last page).
  12. 连接完全启动后,查看您是否有cookie,会话或某种类型的数据表示您是谁=> TryCreateUser(userData)(返回您在最后一页上创建的用户)。
  13. Data continues pumping down to user.
  14. 数据继续向用户灌输。

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连接的生命周期内数据不能更改,只能在连接处于断开连接状态时创建/修改数据。