我正在使用AddParameter
,特别是使用自托管服务器和.NET客户端。这是集线器的代码:
request.AddJsonBody(
new
{
UserName = "UAT1206252627",
SecurityQuestion = securityQuestion
}); // AddJsonBody serializes the object automatically
这是简单客户端(控制台应用程序)的代码:
SignalR
问题出在这一行:
[HubName("ChatHub")]
public sealed class ChatHub : Hub
{
public Task JoinGroup(string groupName)
{
Console.WriteLine($"Client {Context.ConnectionId} has joined group {groupName}");
return Groups.Add(Context.ConnectionId, groupName);
}
public Task LeaveGroup(string groupName)
{
Console.WriteLine($"Client {Context.ConnectionId} has leaved group {groupName}");
return Groups.Remove(Context.ConnectionId, groupName);
}
public void Send(string groupName, string message)
{
Console.WriteLine($"A message '{message}' was received for group {groupName}");
Clients.OthersInGroup(groupName).pushMessage(message);
}
}
我期待, static async Task RunClient(string groupName, HubConnection connection)
{
try
{
var proxy = connection.CreateHubProxy("ChatHub");
proxy.On<string>("pushMessage", _ => Console.WriteLine(_));
await connection.Start();
await proxy.Invoke("JoinGroup", groupName);
string message = null;
do
{
message = Console.ReadLine();
await proxy.Invoke("Send", groupName, message);
}
while (message != "quit");
await proxy.Invoke("LeaveGroup", groupName);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
将推送消息发送给客户端,并触发操作以向控制台显示消息。但客户端只有在向服务器发送内容时才会收到消息。
例如,如果我将运行两个客户端,则序列将如下所示:
是否可以将消息推送到客户端,而无需从该客户端向服务器发送内容?