2014-12-02 94 views
2

获取返回值我都用的请求发送到特定的客户端,然后得到一个返回值的方法SignalR。我知道这是不可能的,但客户端需要将响应作为单独的消息发送到集线器。从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; 
} 

HUB类

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

我如何可以等待客户端的响应,并在我的方法使用该响应的枢纽外?

回答

3

一旦你的集线器连接,你必须等待并聆听了答案:

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

当然,你必须保持该连接活着。

+0

太好了!那是我错过的方法! – 2014-12-03 07:57:01

+0

但抱歉,在IHubContext中找不到On方法。 – 2014-12-03 19:39:25