SignalR 私人聊天不返回数据
SignalR Private Chat Not Returning Data
我在我的项目中实现了 SignalR。但是我在发送私信时遇到问题。我正在从页面发送 'toConnectionID'。
ChatHub.cs
public void LoadPrivateMessages(string toUserName,string toConnectionID)
{
var chatHub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
string fromUserName = Context.QueryString["userName"].ToString();
string fromConnectionID = Context.ConnectionId;
List<ChatMessageDetail> currentMessages = cachePrivateMessages.Where(x => x.ToUserName == toUserName && x.FromUserName == fromUserName && x.ToConnectionID==toConnectionID && x.FromConnectionID==fromConnectionID).ToList();
chatHub.Clients.Client(toConnectionID).privateMessagesHistory(currentMessages);
}
我的当前消息列表正在填充。我在这里很好。但是我无法将消息发送到页面。
Javascript
chatHub.client.privateMessagesHistory = function (data) {
console.log(data);
};
此 JavaScript 代码后我的控制台屏幕为空。
编辑:
那是我在 document.ready 函数中的连接代码。其次是发送接收者的信息。
var chatHub = $.connection.chatHub;
$.connection.hub.qs = { "userName": "@Session["userName"]" };
$.connection.hub.start().done(function () {
console.log("Connection OK !");
});
$(document).on('click','.onlineUser', function () {
var userName = $(this).attr('id');//That is which user to send(Receiver)
var toConnectionID = $(this).attr('connectionID');
chatHub.server.loadPrivateMessages(userName, toConnectionID);
$('.privateMessagediv').show();
$('.userName').html("");
$('.userName').append("<h4>" + userName + "</h4>");
$('.btnSendingPrivateMessage').attr('id', userName);
$('.btnSendingPrivateMessage').attr('connectionid', toConnectionID);
chatHub.client.privateMessagesHistory = function (data) {
$('#privateChatBox').append(data);
};
});
编辑 2:
我解决了问题。
chatHub.Clients.Client(fromConnectionID).privateMessagesHistory(currentMessages);
而不是
chatHub.Clients.Client(toConnectionID).privateMessagesHistory(currentMessages);
我最初的怀疑是 toConnectionId
,但在检查了您的 privateMessageHistory
回调注册后,它更有可能是时间问题。
您需要在集线器之前设置客户端回调 .start()
。
Normally you register event handlers before calling the start method
to establish the connection. If you want to register some event
handlers after establishing the connection, you can do that, but you
must register at least one of your event handler(s) before calling the
start method
无需在点击事件中进行回调注册。
var chatHub = $.connection.chatHub;
$.connection.hub.qs = { "userName": "@Session["userName"]" };
// callback first
chatHub.client.privateMessagesHistory = function (data) {
$('#privateChatBox').append(data);
};
// start the hub connection - note chatHub is different from your .hub
chatHub.start().done(function () {
console.log("Connection OK !");
});
我在我的项目中实现了 SignalR。但是我在发送私信时遇到问题。我正在从页面发送 'toConnectionID'。
ChatHub.cs
public void LoadPrivateMessages(string toUserName,string toConnectionID)
{
var chatHub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
string fromUserName = Context.QueryString["userName"].ToString();
string fromConnectionID = Context.ConnectionId;
List<ChatMessageDetail> currentMessages = cachePrivateMessages.Where(x => x.ToUserName == toUserName && x.FromUserName == fromUserName && x.ToConnectionID==toConnectionID && x.FromConnectionID==fromConnectionID).ToList();
chatHub.Clients.Client(toConnectionID).privateMessagesHistory(currentMessages);
}
我的当前消息列表正在填充。我在这里很好。但是我无法将消息发送到页面。
Javascript
chatHub.client.privateMessagesHistory = function (data) {
console.log(data);
};
此 JavaScript 代码后我的控制台屏幕为空。
编辑:
那是我在 document.ready 函数中的连接代码。其次是发送接收者的信息。
var chatHub = $.connection.chatHub;
$.connection.hub.qs = { "userName": "@Session["userName"]" };
$.connection.hub.start().done(function () {
console.log("Connection OK !");
});
$(document).on('click','.onlineUser', function () {
var userName = $(this).attr('id');//That is which user to send(Receiver)
var toConnectionID = $(this).attr('connectionID');
chatHub.server.loadPrivateMessages(userName, toConnectionID);
$('.privateMessagediv').show();
$('.userName').html("");
$('.userName').append("<h4>" + userName + "</h4>");
$('.btnSendingPrivateMessage').attr('id', userName);
$('.btnSendingPrivateMessage').attr('connectionid', toConnectionID);
chatHub.client.privateMessagesHistory = function (data) {
$('#privateChatBox').append(data);
};
});
编辑 2:
我解决了问题。
chatHub.Clients.Client(fromConnectionID).privateMessagesHistory(currentMessages);
而不是
chatHub.Clients.Client(toConnectionID).privateMessagesHistory(currentMessages);
我最初的怀疑是 toConnectionId
,但在检查了您的 privateMessageHistory
回调注册后,它更有可能是时间问题。
您需要在集线器之前设置客户端回调 .start()
。
Normally you register event handlers before calling the start method to establish the connection. If you want to register some event handlers after establishing the connection, you can do that, but you must register at least one of your event handler(s) before calling the start method
无需在点击事件中进行回调注册。
var chatHub = $.connection.chatHub;
$.connection.hub.qs = { "userName": "@Session["userName"]" };
// callback first
chatHub.client.privateMessagesHistory = function (data) {
$('#privateChatBox').append(data);
};
// start the hub connection - note chatHub is different from your .hub
chatHub.start().done(function () {
console.log("Connection OK !");
});