How to disconnect specified client from outside hub
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
Hi,
I have the exact same need of this issue: https://github.com/dotnet/aspnetcore/issues/5333, but sadly the microsoft bot closed it for inactivity.
When i ban a user, i put his user-id in a table to be able to block further connection from this user. But if the user is already connected, he can still send and receive message. I could add a filter to ignore all request for this user, but he will still be able to receive all the message.
The proposed solution seem to create a « request disconnect » message in user code.
```
// backend
_hubContext.Clients.User("user-id-here").RequestDisconnect();
//frontend
const connection: HubConnection = new HubConnectionBuilder()
.withUrl('/hub/myHub', {
accessTokenFactory: async () => (await getAccessToken()) ?? '',
transport: HttpTransportType.WebSockets,
skipNegotiation: true
})
.withAutomaticReconnect()
.build();
connection.on('RequestDisconnect', () => {
connection.stop();
});
```
But i don't like that solution, a user could have a client that doesn't respect the requestDisconnect. The other solution seem to
seem to manually track the user connection in the hub itself and to be able to do a .Abort() on it later:
https://github.com/dotnet/aspnetcore/issues/5333#issuecomment-564702805
> There is likely a workaround way to implement this right now. The necessary logic should already exist in the Hub itself (via Context.Abort()). The request here is to be able to do it from outside the hub.
>
>While we don't generally recommend doing this, it is currently OK to capture the HubCallerContext (from the Context property) in OnConnectedAsync, store it somewhere (a dictionary indexed by ConnectionID perhaps) and then call .Abort() on it to terminate the connection.
>
>I do agree that we should look at a more first-class way to do this, but I'd be curious if this workaround works for your scenario. Would you be able to try it and report back? We'll look at this for 5.0, but that's not coming until November 2020 and I'd like to be able to give you something that works before then :).
Is that any way that this functionality could be implemented in the Signalr library itself ?
In the context of my application we only send message to groups, so technically i could only remove the user from the all the groups that he is part of, but even that can't work because the group manager doesn't all to remove user from group, only connection.
Contributor guide
Assessment
This issue has not been assessed yet.