SignalR (v2.2.0) OnDisconnected disconnected the user

I use the following code to add a user to a group and save the user in db for that particular group using the following code.

SERVER:

  public class ChatHub : Hub
{


    public async Task JoinRoom(string user_Id, string room_Id, string user_Name)
    {
        AddLoginUser(room_Id, this.Context.ConnectionId, user_Id);
        await this.Groups.Add(this.Context.ConnectionId, room_Id);
    }


    public void Connect(string user_Id, string room_Id, string user_Name)
    {
        var id = Context.ConnectionId;

        Clients.Caller.onConnected(id, user_Name, GetRoomUser(room_Id), GetRoomMessage(room_Id));

        // send to all in group to update user list
        Clients.OthersInGroup(room_Id).onNewUserConnected(id, user_Name);
    }
  public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
    {

        using (DataContext dc = new DataContext())
        {
            var item = dc.LoggedInUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
            if (item != null)
            {
                item.Connected = false;
                dc.SubmitChanges();

                Clients.OthersInGroup(item.RoomID.ToString()).onUserDisconnected(Context.ConnectionId, item.UserMaster.User_Name);
            }

            return base.OnDisconnected(stopCalled);
        }
    }
   }

 private void AddLoginUser(string room_Id, string connection_Id, string user_Id)
    {
        using (DataContext dc = new DataContext())
        {
            var checkUserLogedIn = (from user in dc.LoggedInUsers
                                    where (user.RoomID == Convert.ToInt32(room_Id) && user.UserID == Convert.ToInt32(user_Id))
                                    select user).SingleOrDefault();
            if (checkUserLogedIn == null)
            {
                LoggedInUser objLoggedInUser = new LoggedInUser();
                objLoggedInUser.ConnectionId = connection_Id;
                objLoggedInUser.UserID = Convert.ToInt32(user_Id);
                objLoggedInUser.RoomID = Convert.ToInt32(room_Id);
                objLoggedInUser.Connected = true;
                dc.LoggedInUsers.InsertOnSubmit(objLoggedInUser);
                dc.SubmitChanges();
            }
            else
            {
                if (!checkUserLogedIn.Connected)
                {
                    checkUserLogedIn.Connected = true;
                    dc.SubmitChanges();
                }
            }
        }
    }

Problem:

Suppose I logged in with userid = 1 for roomid = 1 and contextid = 123asd. If I update my window, the context will change, and now, if I close the browser tab, and then the following request:

var item = dc.LoggedInUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);

I don’t recognize the user from the last connectionid, because when I saved the user on the connection, the connectionid was different at that time.

How can I set the connection status to false for a specific user when disconnected.

Thanks in advance.

+1
source share
1

OnConnected connectionIds ( ), connectionId , . signalr ( ).

, connectionId OnConnected. connectionId, connectionIds OnDisconnected. connectionId , ( , , , ) OnReconnected.

. -, connectionId. ; ( ) , .

, . AddLoginUser OnReconnected.

     public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
                {
                    using (DataContext dc = new DataContext())
                    {
                        var item = dc.LoggedInUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
                        if (item != null)
                        {
                            dc.LoggedInUsers.Remove(item);
                            dc.SubmitChanges();
//If there is no other connection left with this user in this room send message.
                               if (!dc.LoggedInUsers.Any(x => x.RoomID==item.RoomID && x.userId==item.UserId)
                                   Clients.OthersInGrouproomId.ToString()).onUserDisconnected(Context.ConnectionId, item.UserMaster.User_Name);
                           }
                        return base.OnDisconnected(stopCalled);
                    }
                }
            }

            private void AddLoginUser(string room_Id, string connection_Id, string user_Id)
            {
                using (DataContext dc = new DataContext())
                {
//Just check connectionId uniqunes. You don't need connected field.
                    var checkUserLogedIn = (from user in dc.LoggedInUsers
                                            where user.ConnectionId == connection_Id
                                            select user).SingleOrDefault();
                    if (checkUserLogedIn == null)
                    {
                        LoggedInUser objLoggedInUser = new LoggedInUser();
                        objLoggedInUser.ConnectionId = connection_Id;
                        objLoggedInUser.UserID = Convert.ToInt32(user_Id);
                        objLoggedInUser.RoomID = Convert.ToInt32(room_Id);
                        dc.LoggedInUsers.InsertOnSubmit(objLoggedInUser);
                        dc.SubmitChanges();
                    }
                }
            }
0

Source: https://habr.com/ru/post/1531502/


All Articles