[swift]SignalR(v2.2.0) OnDisconnected 设置的用户脱机

标签: ios Swift C#
发布时间: 2017/3/5 20:06:46
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我用下面的代码在组中添加用户并将用户保存在 db 反对这一特定群体使用以下代码。

服务器︰

  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();
                }
            }
        }
    }

问题︰

假设我登录的用户 id = 1 为 roomid = 1 和关联 = 123asd。如果我刷新我的窗前那么的关联会改变现在如果我关闭浏览器选项卡然后以下查询︰

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

不找出用户对最新的 connectionid,因为当我在救了用户连接那时候 connectionid 是不同。

如何为特定用户的连接的状态 false 可以出发断开连接事件。

在此先感谢。

解决方法 1:

OnConnected您应保存所有的 connectionIds (这使用用户映射),connectionId 应该是唯一的不是用户。因为用户可以具有多个连接到那么 signalr 在同一时间 (新的选项卡)。

每次您应该映射用户和 connectionId 上 Onconnected 。每次你应该只是删除那 connectionId,并不是所有的用户 connectionIds OnDisconnected 。如果它不在列表中,您应与用户添加 connectionId (如果停止叫不叫断开连接会出现即使用户没有断开连接) 上 OnReconnected

你应该重构您的代码在此基础之上。首先,你应该删除 connectionId。然后,您可以检查;如果留下这个用户 (这映射与那 connectionId) 名单上没有记录,您可以发送消息。

检查在这里

我有点改变了您的代码,你可以提高此代码基于这方面的知识。你应该调用 AddLoginUserOnReconnected 也。

     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();
                    }
                }
            }
官方微信
官方QQ群
31647020