我正在尝试在.NET客户端中使用SignalR。我能够成功连接,但当我尝试订阅事件时没有任何反应。我无法接收从服务器发送的消息。我对这个领域很陌生,所以我不确定我是否遗漏了什么或我的代码中有什么问题。
var hubConnection = new HubConnection(<url>);
// Hub name from dev code
var proxy = hubConnection.CreateHubProxy("pidHub");
// Subscribing to all the events
proxy.On("SendTransactionUpdate", () => Logger.LogInformation("SendTransactionUpdate ************************"));
proxy.On("SendMessage", () => Logger.LogInformation("SendMessage ************************"));
proxy.On("UpdateStatus", () => Logger.LogInformation("UpdateStatus ************************"));
proxy.On("TransactionUpdate", () => Logger.LogInformation("TransactionUpdate ************************"));
// This block executes fine
hubConnection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Logger.LogInformation("There was an error opening the connection: {0}" + task.Exception.GetBaseException());
}
else
{
Logger.LogInformation("Connected. *********");
}
}).Wait();
// Call Provision GUID API and returns a valid GUID
<call API>
// Downloads EXE on machine
<download EXE code>
// Run EXE with -d option
// EXE uploads results back to the server and server returns some messages through SignalR
// I want to capture these messages
<execute EXE on machine>
- 您可能希望在任务未出错时订阅任务继续上的事件。
- 如果您已成功连接到服务器,则可能需要使用提琴手来跟踪服务器消息。
- 确保事件处理程序包含从服务器发送的参数的参数(如果有)。您的应用程序可能会收到消息,但处理错误。