我是signalR和angularJS的新手。我最近在asp.net MVC5中使用signalR和angularJs开发了聊天应用程序。
我将Context.ConnectionId更改为用户登录ID。所以每次刷新,ConnectionId都不会刷新。我可以查看所有登录/在线用户,我可以通过选定的用户发送消息,但接收者看不到发送者发送的消息。
在ChatHub.cs中
public void SendPrivateMessage(long toUserId, string message,long connid)
{
long fromUserId =Convert.ToInt32(connid);
var toUser = loggedInUsers.FirstOrDefault(x => x.ConnectionId == toUserId);
var fromUser = loggedInUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);
if (toUser != null && fromUser != null)
{
//
Clients.Caller.sendPrivateMessage(toUserId, fromUser.Username, message);
Clients.Client(Convert.ToString(toUserId)).sendPrivateMessage(fromUserId, fromUser.Username, message);
}
}
在Controler.js
signalR.RecievingPrivateMessage(function (toname, fromname, msg) {
console.log($scope.ShowPrivateWindow == false);
if ($scope.ShowPrivateWindow == false) {
$scope.ShowPrivateWindow = true;
}
// var msgBdy = { room: r, msgx: { message: msg.message, sender: msg.sender, css: msg.css } };
//$scope.chatHistory.push(msgBdy);
$scope.PrivateMessages.push({ to: toname, from: fromname, message: msg });
console.log($scope.PrivateMessages);
if ($scope.$parent.UserName != fromname) // otheruser's pm
{
console.log('here');
if ($scope.UserInPrivateChat == null)
{
$scope.UserInPrivateChat = { Username: fromname, ConnectionId: toname }
}
}
/// To Scroll the message window.
if ($("#PrivateChatArea div.panel-body").length == 1) {
var $container = $("#PrivateChatArea div.panel-body");
$container[0].scrollTop = $container[0].scrollHeight;
$container.animate({ scrollTop: $container[0].scrollHeight }, "fast");
}
$scope.$evalAsync();
// $scope.AddMessageToRoom(msgBdy);
});
这里会出现什么问题?我无法追踪错误。