Signalr Connect Disconnect and reconnect

I want to make sure that I am implementing the group function correctly for the SignalR library.

What I am doing is allowing users to request help for a specific project. The user who started the project can add other users to the collaboration sheet for their project.

Collaboration 
(
    UserID Uniqueidentifier,
    ProjectID INT
)

      

If a user goes into collaboration mode, I want to add that user to the group, so if another user logs on and goes into collaboration mode, they are added to the same group. Groups are always named ProjectID

.

So, when a user logs in and opens projects, if that project is in the collaboration table, I add them to Groups.Add(Conext.ConnectionId,projID)

;


Here are my questions:

When a user connects to a client and OnConnected is called, if no group with projID exists, will this raise an error or signal that just create this group on the fly?

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

      

When the client closes their browser, is that when OnDisconnected is called? And if this user is not a member of the specified projID group for some reason, is this throw and error or will it signal it?

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

      

For OnReconnected, this means that if the user logs off and does something else, then it is again recorded that they are automatically added back to the group, where is the part before the connection was lost?

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

      

For all of the above methods, do I need to call the base method for each overridden method?

+3


source to share


1 answer


  • SignalR will create the group when you call Groups.Add()

    for the first time for a specific projID

    . This will not result in an error.
  • OnDisconnected

    called whenever the connection goes away. if you call Stop()

    there is a clean disconnect and the OnDisconnected

    method is called immediately. If you just close the browser, the Method is OnDisconnected

    usually called after a delay of about 30 seconds (there is a config switch for this)
  • Users are assigned to specific groups based on their connection IDs. If the user comes back with a different connection id you will have to add him again to the appropriate group. You can take a look at the Sample Chat provided by SignalR to see how such cases can be handled.


+7


source







All Articles