[英]SignalR when page is refreshed
I am using SignalR to share user's live location and I run into a big problem and I can't find a good idea to get rid of a problem.我正在使用 SignalR 分享用户的实时位置,我遇到了一个大问题,我找不到解决问题的好主意。 The problem is that every time a user starts sharing the location I add them into the database table in which I keep the people sharing their location, but if this user refreshes the page I don't know this and they remains as a user that shares the location in the database.
问题是,每次用户开始共享位置时,我都会将他们添加到数据库表中,我让人们在其中共享他们的位置,但如果该用户刷新页面,我不知道这一点,他们仍然是共享的用户数据库中的位置。 If they just leaves the page or press a stop button they are erased from the database.
如果他们只是离开页面或按下停止按钮,他们就会从数据库中删除。
Any idea on how to solve this problem if it is possible?如果可能的话,关于如何解决这个问题的任何想法?
but if this user refreshes the page I don't know this
但是如果这个用户刷新页面我不知道
You can use Hub.OnConnectedAsync Method to get the refreshed Connectionid, and according to the current user update the refreshed Connectionid to the database, code like below:可以使用Hub.OnConnectedAsync 方法获取刷新后的Connectionid,并根据当前用户将刷新后的Connectionid更新到数据库中,代码如下:
public override async Task OnConnectedAsync()
{
//get the connection id
var connectionid = Context.ConnectionId;
//get the username or userid
var username = Context.User.Identity.Name;
var userId = Guid.Parse(Context.User.FindFirstValue(ClaimTypes.NameIdentifier));
//update the current user's connectionid to database
var CId = _context.UserIdToCId.Find(userId);
CId.ConnectionId = connectionid;
_context.Update(CId);
await _context.SaveChangesAsync();
await base.OnConnectedAsync();
}
Note Remember to go to the database to check the latest ConnectionId of this person before share user's live location.注意分享用户实时位置前记得先go到数据库查看此人最新的ConnectionId。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.