gpt4 book ai didi

c# - Signalr:当用户为特定用户发送消息时无法发回消息

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:48 25 4
gpt4 key购买 nike

我是 SignalR 的新手。当我向特定用户发送消息时:

  1. 我没有收到消息(我没有在屏幕上看到它)。
  2. 用户收到通知不能回发消息。

这意味着只能向一个方向发送消息。如何做到两个用户可以互相发送消息?**

我使用的是我的类(class):

[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>:&nbsp;&nbsp;' + 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;
}

谁有办法帮助我

最佳答案

如果我理解正确的话,你可以像这样向特定用户发送消息。

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);
}

“string who”应该是接收者用户名。希望对您有所帮助。

关于c# - Signalr:当用户为特定用户发送消息时无法发回消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27349059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com