我在更改页面时遇到保存连接的问题。我有一页显示房间列表,第二页显示聊天视图。当我在连接页面上加入连接时加入方法,之后当我选择加入哪个房间时,它会转到ondisconnected然后是JoinGroups然后是Onconnect方法。请告诉我如何连接两个页面?
这里是chathub的javascript:
$(function () {
// Declare a proxy to reference the hub.
var chatHub = $.connection.ChatHub;
registerClientMethods(chatHub);
// Start Hub
$.connection.hub.start().done(function () {
roomsEvent(chatHub);
authorize(chatHub);
registerEvents(chatHub);
});
});
function authorize(hub) {
var name = $('#txtMessage').prop('Placeholder');
hub.server.connect(name);
$('#displayname').val(name);
messageClearFocus();
$('#txtMessage').prop('placeholder', '');
}
function roomsEvent(hub) {
$('#rooms ul li').not('.nav-header').click(function () {
$('#rooms ul li').not('.nav-header').removeClass('active');
$(this).addClass('active');
var roomKey = $(this).data('value');
hub.server.joinGroup(roomKey);
});
}
和Connect方法:
public void Connect(string userName)
{
userName = Context.User.Identity.Name;
if (ConnectedUsers.Any(x => x.ConnectionId == Context.ConnectionId))
return;
//Add a new user to the list
var user = new ChatUser
{
ConnectionId = Context.ConnectionId,
userLogin = userName
};
//Add to maintained user list
ConnectedUsers.Add(user);
//Add to default group
//await
Groups.Add(user.ConnectionId, user.CurrentGroup);
//Give the current group an updated list of users
UpdateGroupUserList(user.CurrentGroup);
//Alert others that user joined
var message = string.Format("{0} has joined.", user.userLogin);
SendGroupAlert(user, message);
//Alert user that they have joined
var personalMessage = string.Format("You have joined {0} as {1}.", user.CurrentGroup, user.userLogin);
SendPersonalAlert(personalMessage);
}