如何从Hub中删除用户?

时间:2021-02-20 19:43:38

I'm sending by ajax post request to a method in the controller a string 'userName' that I should kick. Is it possible to remove the user from current hub calling the method in the controller?

我通过ajax post请求向控制器中的方法发送一个字符串“userName”,我应该删除它。是否可以从调用控制器中的方法的当前hub中删除用户?

  public ActionResult Kick(string userName)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        var user = userService.GetUserByName(userName);
        var room = chatRoomService.GetRoomById(user.ChatRoomId.Value);
        user.IsKicked = true;
        userService.LeaveRoom(user);
        hubContext.Groups.Remove(user.ConnectionIdInHub, room.Name);
        return Json(new {success = true});
    }

Could i somewhere in this method disconnect user from hub?

我可以在这个方法的某个地方断开用户与hub的连接吗?

1 个解决方案

#1


2  

Server Side-

You should store user's connection ID at the time of his connection.

您应该在用户连接时存储其连接ID。

Like this in server side-

像这样在服务器端-

public override Task OnConnected()
{
    Boolean isFoundAnydevice = false;

    if(receivedClientId.Length>0)   //With Param
    {
        int noOfSelectedDevice = _context.TargatedDevice.Where(x => x.PhoneId == receivedClientId).Count();
        if (noOfSelectedDevice > 0)
            isFoundAnydevice = true;
    }
    else   //With no Param
    {
        String deviceId = _context.Device.Where(d => d.ConnectionId == this.Context.ConnectionId).Select(d => d.ClientId).SingleOrDefault();
    int noOfSelectedDevice = _context.TargatedDevice.Where(x => x.PhoneId == deviceId).Count();
        if (noOfSelectedDevice > 0)
            isFoundAnydevice = true;
    }

    if (isFoundAnydevice)
    {
        _logger.LogWarning(
                        receivedClientId + " added to Test group"
                        );
        Groups.Add(this.Context.ConnectionId, testGroupName);
    }   

    return base.OnConnected();
}

Then you can easily find the user's connection ID from DB.

然后,您可以很容易地从DB找到用户的连接ID。

Now you can easily stop the hub connection like this-

现在你可以很容易地停止像这样的集线器连接

public Task Disconnect(string connectionId)
{
    try
    {
        lock (_lock)
        {
            var connections = _registeredClients.Where(c => c.Value.Any(connection => connection == connectionId)).FirstOrDefault();

            // if we are tracking a client with this connection 
            // remove it
            if (!CollectionUtil.IsNullOrEmpty(connections.Value))
            {
                connections.Value.Remove(connectionId);

                // if there are no connections for the client, remove the client from the tracking dictionary
                if (CollectionUtil.IsNullOrEmpty(connections.Value))
                {
                    _registeredClients.Remove(connections.Key);
                }
            }
        }
    }
    catch (Exception ex)
    {
        Log.Error(this, "Error on disconnect in hub", ex);
    }

    return null;
}

More can be found in here.

在这里可以找到更多。

Client Side-

If you like to do it from client side, you can do this-

如果你喜欢在客户端做,你可以做这个-

$.connection.hub.stop();

Hope you have your answer

希望你有答案

#1


2  

Server Side-

You should store user's connection ID at the time of his connection.

您应该在用户连接时存储其连接ID。

Like this in server side-

像这样在服务器端-

public override Task OnConnected()
{
    Boolean isFoundAnydevice = false;

    if(receivedClientId.Length>0)   //With Param
    {
        int noOfSelectedDevice = _context.TargatedDevice.Where(x => x.PhoneId == receivedClientId).Count();
        if (noOfSelectedDevice > 0)
            isFoundAnydevice = true;
    }
    else   //With no Param
    {
        String deviceId = _context.Device.Where(d => d.ConnectionId == this.Context.ConnectionId).Select(d => d.ClientId).SingleOrDefault();
    int noOfSelectedDevice = _context.TargatedDevice.Where(x => x.PhoneId == deviceId).Count();
        if (noOfSelectedDevice > 0)
            isFoundAnydevice = true;
    }

    if (isFoundAnydevice)
    {
        _logger.LogWarning(
                        receivedClientId + " added to Test group"
                        );
        Groups.Add(this.Context.ConnectionId, testGroupName);
    }   

    return base.OnConnected();
}

Then you can easily find the user's connection ID from DB.

然后,您可以很容易地从DB找到用户的连接ID。

Now you can easily stop the hub connection like this-

现在你可以很容易地停止像这样的集线器连接

public Task Disconnect(string connectionId)
{
    try
    {
        lock (_lock)
        {
            var connections = _registeredClients.Where(c => c.Value.Any(connection => connection == connectionId)).FirstOrDefault();

            // if we are tracking a client with this connection 
            // remove it
            if (!CollectionUtil.IsNullOrEmpty(connections.Value))
            {
                connections.Value.Remove(connectionId);

                // if there are no connections for the client, remove the client from the tracking dictionary
                if (CollectionUtil.IsNullOrEmpty(connections.Value))
                {
                    _registeredClients.Remove(connections.Key);
                }
            }
        }
    }
    catch (Exception ex)
    {
        Log.Error(this, "Error on disconnect in hub", ex);
    }

    return null;
}

More can be found in here.

在这里可以找到更多。

Client Side-

If you like to do it from client side, you can do this-

如果你喜欢在客户端做,你可以做这个-

$.connection.hub.stop();

Hope you have your answer

希望你有答案