我是新使用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;
}
任何人有解决方案来帮助我
'Clients.Client(connectionId).addChatMessage(name +“:”+ message);'in javascript'chat.client.addChatMessage = function(name,message )'c sharp call should be'Clients.Client(connectionId).addChatMessage(name,message);' – Moes 2014-12-07 23:52:55