2
votes

I have a SignalR method used to send a request to a specific client and then get a return value. I know this isn't possible, but instead the client needs to send the response as a separate message to the hub.

I send the message to the client from outside the hub:

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

Hub class

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

How can I wait for the client response and use this response in my method outside the hub?

1

1 Answers

3
votes

Once your hub is connected you have to wait and listen for the answer:

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

Of course you have to keep that connection alive.