所以我创建了一个包含在线用户列表的站点母版页 但现在每次我导航到另一个页面或刷新页面,它刷新所有连接的客户端列表.. 它真的很烦人,我怎么能避免呢?
public override Task OnDisconnected(bool stopCalled)
{
if (stopCalled)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindByName(Context.User.Identity.Name);
string name = currentUser.NickName;
userNames.Remove(name);
Clients.All.hubReceived(userNames);
}
return base.OnDisconnected(stopCalled);
}
这是我的脱离行动
以及客户方:
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var hub = $.connection.chatHub;
// Update player list
hub.client.hubReceived = function (usernames) {
$('#onlineUsersList').text("");
console.log(usernames.length)
if (usernames.length > 0) {
for (var i = 0; i < usernames.length; i++) {
if (usernames[i] != "")
$("#onlineUsersList").append("<div class='userli'> <img src='Content/Images/profilepic.jpg' style='width: 32px; height: 32px;'/><span>" + usernames[i] + "</span></div>")
// $('#usersList').append(usernames[i]);
}
}
};
hub.client.test = function (x) {
alert(x);
};
$.connection.hub.start();
});
</script>