简体   繁体   English

信号器连接断开并重新连接

[英]Signalr Connect Disconnect and reconnect

I am wanting to make sure that I am implementing the group feature correctly for the SignalR library. 我想确保我正在为SignalR库正确实现组功能。

What I am doing is allowing users to ask for help for a specific project. 我正在做的是允许用户为特定项目寻求帮助。 The user that started the project can add other users to a collaboration table for their project. 启动项目的用户可以将其他用户添加到其项目的协作表中。

Collaboration 
(
    UserID Uniqueidentifier,
    ProjectID INT
)

If either user goes into collaboration mode I want to add that user to a group so if another user logs on and goes into collaboration mode they are added to the same group. 如果任一用户进入协作模式,我想将该用户添加到组中,因此如果其他用户登录并进入协作模式,则会将其添加到同一组中。 The groups are always named the ProjectID . 这些组始终命名为ProjectID

So when a user logs on and opens a projects if that project is in the collaboration table I add them to the Groups.Add(Conext.ConnectionId,projID) ; 因此,当用户登录并打开项目时,如果该项目在协作表中,我将它们添加到Groups.Add(Conext.ConnectionId,projID) ;


Here are my questions: 这是我的问题:

When a user connects from the client and the OnConnected is called if no group with the projID exists will this throw an error or will signalr just create that group on the fly? 当用户从客户端连接并且如果没有存在projID的组时调用OnConnected,则会抛出错误或者信号器只是动态创建该组?

    public override Task OnConnected(string projID)
    {
        return Groups.Add(this.Context.ConnectionId, projID);
    }

When a client closes their browser, is that when the OnDisconnected is called? 当客户端关闭浏览器时,是否在调用OnDisconnected时? And if that user for some reason is not in the said projID group will this throw and error or will signalr handle this? 如果那个用户由于某种原因不在所说的projID组中,那么这会抛出错误还是会让信号器处理这个?

    public override Task OnConnected(string projID)
    {
        return Groups.Add(this.Context.ConnectionId, projID);
    }

For the OnReconnected, does this mean that if a user logs off and does something else then logs back on that they are automatically added back to the group they where part of before the connect was lost? 对于OnReconnected,这是否意味着如果用户注销并执行其他操作,那么重新登录它们会自动添加回到连接丢失之前的部分组中?

    public override Task OnReconnected(string projID)
    {
        return Clients.Group(projID).rejoined(Context.ConnectionId,
            DateTime.Now.ToString());
    }

For all of the methods above do I need to call the base method of each overrided method? 对于上述所有方法,我是否需要调用每个覆盖方法的基本方法?

  1. SignalR will create the group when you call Groups.Add() for the first time for a particular projID . 当您第一次为特定projID调用Groups.Add()时,SignalR将创建该组。 It won't throw an error. 它不会抛出错误。
  2. OnDisconnected is called whenever the connection goes away. 只要连接消失,就会调用OnDisconnected If you call Stop() there is a clean disconnect and the OnDisconnected method is called immediately. 如果调用Stop()则会有一个干净的断开连接,并立即调用OnDisconnected方法。 If you just shut the browser, the OnDisconnected method will usually be called after a delay of about 30 seconds (there's a configuration switch to control this) 如果您只是关闭浏览器,通常会在大约30秒的延迟后调用OnDisconnected方法(有一个配置开关来控制它)
  3. Users are tied to specific groups based on their connection id's. 用户根据其连接ID绑定到特定组。 If a user comes back with a different connection id you will have to add it again to the appropriate group. 如果用户使用不同的连接ID返回,则必须将其再次添加到相应的组中。 You can have a look at the Chat sample provided with SignalR to see how cases such as this can be handled. 您可以查看SignalR提供的Chat示例,以了解如何处理此类案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM