SignalR(V2.2.0)OnDisconnected设置用户下线 [英] SignalR(v2.2.0) OnDisconnected set user offline
问题描述
我使用下面的代码在组中添加用户,并保存用户对使用下面的代码这一特定群体分贝
I am using the following code to add user in group and save user in db against this particular group using the following code.
服务器:
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();
}
}
}
}
< STRONG>问题:
假设我登录的用户ID与= 1 roomid = 1,关联标识符= 123asd。如果我刷新我的窗前,然后关联标识符会改变,现在如果我关闭浏览器选项卡,然后下面的查询:
Suppose i logged-in with userid=1 for roomid=1 and contextid=123asd. If i refresh my window then contextid will change and now if i closing browser tab then following query:
var item = dc.LoggedInUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
找不到反对最新的ConnectionId用户,因为当我有保存的用户当时的ConnectionId连接是不同的。
not find out the user against latest connectionid, because when i had saved user on connect at that time connectionid was different.
如何设置连接的状态假就断开事件特定的用户。
How i can set connected status false for particular user on disconnect event.
在此先感谢
推荐答案
OnConnected
您应该保存所有connectionIds(它映射与用户)的ConnectionId应该是唯一的,而不是用户。由于用户可以有多个连接在同一时间(新建选项卡),以signalr。
OnConnected
you should save all connectionIds (which is mapped with user), connectionId should be unique, not the user. Because a user can have more than one connection to signalr at the same time(New Tabs).
每当你应该在 Onconnected映射用户和的ConnectionId
。每当你应该删除的ConnectionId,而不是 OnDisconnected
的用户的所有connectionIds。你应该添加用户的ConnectionId,如果它不在列表中(如果停止叫不叫断开偶可发生用户不是断开)在 OnReconnected
。
Everytime you should map user and connectionId on Onconnected
. Everytime you should just remove that connectionId, not all connectionIds of user on OnDisconnected
. You should add connectionId with user if it's not in list(if stop called is not called disconnected can occur even user is not disconnected) on OnReconnected
.
您应该重构这个代码库。首先,你应该删除的ConnectionId。然后,您可以检查;如果有没有留下记录与该用户(它映射与的ConnectionId)的列表中,您可以发送邮件
You should refactor your code base on this. First, you should remove connectionId. Then, you can check; if there is no record left with this user(which is mapped with that connectionId) on list, you can send message.
检查的这里
我已经改变你的代码一点,可以提高基于这种认识这个代码。你应该叫 AddLoginUser
在 OnReconnected
也。
I have changed your code a bit, you can improve this code based on this knowledge. You should call AddLoginUser
on OnReconnected
also.
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();
}
}
}
这篇关于SignalR(V2.2.0)OnDisconnected设置用户下线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!