我是使用SignalR
的新手。当我向特定用户发送消息时:
这意味着发送消息只能在一个方向上。 怎么办两个用户可以互相发送消息?**
我使用的是我的课程:
[HubName("TSChatHub")]
[Authorize]
public class ChatHub : Hub
{
private readonly static ConnectionMapping<string> _connections =
new ConnectionMapping<string>();
public void SendChatMessage(string who, string message)
{
string name = Context.User.Identity.Name;
foreach (var connectionId in _connections.GetConnections(who))
{
Clients.Client(connectionId).addChatMessage(name + ": " + message);
}
}
public override Task OnConnected()
{
string name = Context.User.Identity.Name;
_connections.Add(name, Context.ConnectionId);
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
string name = Context.User.Identity.Name;
_connections.Remove(name, Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}
public override Task OnReconnected()
{
string name = Context.User.Identity.Name;
if (!_connections.GetConnections(name).Contains(Context.ConnectionId))
{
_connections.Add(name, Context.ConnectionId);
}
return base.OnReconnected();
}
}
public class ConnectionMapping<T>
{
private readonly Dictionary<T, HashSet<string>> _connections =
new Dictionary<T, HashSet<string>>();
public int Count
{
get
{
return _connections.Count;
}
}
public void Add(T key, string connectionId)
{
lock (_connections)
{
HashSet<string> connections;
if (!_connections.TryGetValue(key, out connections))
{
connections = new HashSet<string>();
_connections.Add(key, connections);
}
lock (connections)
{
connections.Add(connectionId);
}
}
}
public IEnumerable<string> GetConnections(T key)
{
HashSet<string> connections;
if (_connections.TryGetValue(key, out connections))
{
return connections;
}
return Enumerable.Empty<string>();
}
public void Remove(T key, string connectionId)
{
lock (_connections)
{
HashSet<string> connections;
if (!_connections.TryGetValue(key, out connections))
{
return;
}
lock (connections)
{
connections.Remove(connectionId);
if (connections.Count == 0)
{
_connections.Remove(key);
}
}
}
}
}
有我的观点:
<div class="chatcontainer">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.TSChatHub;
debugger;
// Create a function that the hub can call to broadcast messages.
chat.client.addChatMessage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(name) + '</li>');
};
// Get the user name and store it to prepend to messages.
// $('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
console.log('Now connected, connection ID=' + $.connection.hub.id);
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.sendChatMessage($('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
}).fail(function () { console.log("Could not connect"); });
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
任何人都有解决方案可以帮助我
答案 0 :(得分:0)
如果我正确理解您,您可以向这样的特定用户发送消息。
public void SendChatMessage(string who, string message)
{
string name = Context.User.Identity.Name;
//foreach (var connectionId in _connections.GetConnections(who))
//{
//Clients.Client(connectionId).addChatMessage(name + ": " + message);
//}
Clients.User(who).addChatMessage(name + ": " + message);
}
&#34;字符串&#34;应该是接收者用户名。希望这有帮助。
答案 1 :(得分:0)
只需更改addChatMessage
方法
public void SendChatMessage(string who, string message)
{
string name = Context.User.Identity.Name;
foreach (var connectionId in _connections.GetConnections(who))
{
Clients.Client(connectionId).addChatMessage(name , message);
}
}
这行代码调用您在View上编写的客户端addChatMessage
方法
Clients.Client(connectionId).addChatMessage(name , message);
由于您从cs页面提供的参数与客户端方法的预期不同,因此函数未正确映射。
您的电话
Clients.Client(connectionId).addChatMessage(name + ": " + message);
客户端方法预期的参数
chat.client.addChatMessage = function (name, message)
你提供了一个参数,因此它没有被映射。
如果问题仍然存在,请尝试在_connections
方法中检查SendChatMessage
字典,如果您的连接ID未添加到此字典中,则该消息不会返回给您的唯一原因是可能,因为这是您在OnConnected
事件