SignalR:如何阻止在创建页面重新加载新的连接 [英] SignalR: How to stop creating new connection on page reload

查看:2869
本文介绍了SignalR:如何阻止在创建页面重新加载新的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在开发与我的应用程序的一些其他网页沿着聊天应用程序。有一次我登录我正在维护用户的会话。我的主要目的是,每当另一个用户连接到服务器的用户应该得到通知。

Hi i am developing a chat application along with some other pages in my application. once i login i am maintaining the session of the user. My main intention is that user should get notification whenever another user connects to server.

这是我面临的问题是每当我浏览到我的应用程序其他页面的连接丢失。如何制止这种行为,直到用户注销继续连接。

The problem that i am facing is whenever i navigate to some other page in my application the connection is lost. How to stop this behaviour and continue the connection until user logs out.

我使用SignalR 2.0 ASP.NET MVC4项目,任何帮助?

I am using SignalR 2.0 in ASP.NET MVC4 project, any help??

推荐答案

每个连接只对用户消费金额的页面上的持续时间一个生命周期。当他们浏览到另一个页面,一个新的连接已经建立。此外,如果用户具有一个以上的标签或浏览器窗口打开时,他们将有多个连接ID。我不认为你想尝试坚持连接ID超出它的预期生命周期。

Each connection only has a lifecycle for the duration of the time the user spends on a given page. When they navigate to another page, a new connection is established. Also, if a user has more than one tab or browser window open, they will have multiple connection Ids. I don't think you want to try to persist the connection Id beyond it's intended lifecycle.

在我工作的一个类似的场景中,我们存储connectionIds的onConnect并删除它们OnDisconnect。当需要被发送到给定用户的消息,我们将其发送给所有的connectionIds的。这确保了信息将被传递到所有的标签/窗口。

In a similar scenario that I work on, we store the connectionIds OnConnect and delete them OnDisconnect. When a message needs to be sent to a given user, we send it to all of their connectionIds. This ensures that the message will be delivered to all tabs/windows.

修改1 以下是响应@ SAURABH的评论:

EDIT 1 the following is in response to @Saurabh's comments:

考虑集线器的范围是什么,你的其他类和客户端。集线器有便于浏览器和服务器之间的通信。虽然它可能做了很多工作的中心内,我认为这是最好的移动大部分范围communictions以外的其他地方。

Consider what the scope of the Hub is, and that of your other classes and the client. The Hub is there to facilitate communications between the browser and the server. While it's possible to do a lot of work within the hub, I think it's best to move most of the scope outside of communictions to other places.

客户端知道,它只是重新加载一个页面,所以它作出的决定,这是一个重新连接事件一个很好的候选人。

The client knows that it just reloaded a page, so it's a good candidate for making the decision that this is a reconnect event.

_chatHub.server.reJoinRooms();

则集线器可以查询用户的房间,通过用户ID,而不是的ConnectionId。

Then the Hub can query the user's rooms, by UserId, rather than ConnectionId.

public Task ReJoinRooms()
{
// get the user's rooms from your repository
// optionally get the user's connectionIds from your repository
// Clients.Caller.onJoinRooms(rooms);
// or Clients.Clients(_connectionIds).onJoinRooms(rooms);
}

然后客户端就可以决定是否采取行动:

Then the client can decide whether or not to take action:

$chatModule.client.onJoinRooms = function (rooms) {
   for (var i in rooms) {
           var _room = rooms[i];
           // if I'm not already in this room, add it to my rooms
           console.log('joining room', _room)
       }
}

您可以皮肤这么多不同的方式。客户可以拥有的记忆间的范围,而不是一个服务器端库了。

You could skin this many different ways. The client could own the scope of remembering rooms, instead of a server-side repository, too.

编辑2

如果一个用户所属的不断增长的群体/间数,上面的例子可能不扩展得非常好。

If the number of groups/rooms that a user belongs to is ever-increasing, the above example may not scale up very well.

在这种情况下,每个用户可以加入个人饲料(S),而不是(即加入一个命名为用户的GUID饲料)。我们将保持跟踪隶属于一组每个用户。当邮件发送到该组,我们会遍历这些用户并发布一条消息给每个饲料。

In that case, each user could join personal feed(s) instead (i.e. join a feed named for the user's GUID). We would keep track of each user that is affiliated with a group. When a message is sent to that group, we would iterate over those user's and publish a message to each feed.

这篇关于SignalR:如何阻止在创建页面重新加载新的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆