【问题标题】:Get return value from SignalR client从 SignalR 客户端获取返回值
【发布时间】:2015-01-31 05:20:10
【问题描述】:

我有一个 SignalR 方法用于向特定客户端发送请求,然后获取返回值。我知道这是不可能的,但客户端需要将响应作为单独的消息发送到集线器。

我从集线器外部向客户端发送消息:

public String GetResponseFromUser(String userId, String request)
{
    // Use requestId to be sure the response is on this request
    var requestId = Guid.NewGuid().ToString();

    var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
    hubContext.Clients.User(userId).Request(requestId, request);

    // Wait for client to send response to the Hub's Response method
    var response = ...?

    return response;
}

集线器类

public class MyHub : Hub
{
    public void Response(String requestId, String response)
    {
        // Somehow I want to get the response to the method above
    }
}

如何等待客户端响应并在集线器外部的方法中使用此响应?

【问题讨论】:

    标签: c# signalr


    【解决方案1】:

    连接集线器后,您必须等待并听取答案:

    hubContext.On("Response", (requestId, response) =>
                    {
                        // do something 
                    }
                   );
    

    当然,您必须保持该连接处于活动状态。

    【讨论】:

    • 太棒了!正是我错过的 On 方法!
    • 但是很抱歉,在 IHubContext 中找不到 On 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多