如何在使用HubController <t>?</t>时调用帖子

时间:2013-11-19 04:05:03

标签: signalr

我找不到关于新HubController<T>的很多文档,所以也许我错了。这就是我所拥有的:

public class StatusController : HubController<StatusHub>
{
    private string _status = "";

    public string Get()
    {
        return _status;
    }

    public void Post(string status)
    {
        _status = status;

        // Call StatusChanged on SignalR clients listening to the StatusHub
        Clients.All.StatusChanged(status);
    }
}

public class StatusHub : Hub { }

这就是我尝试创建集线器代理的方式:

var hubConnection = new HubConnection("http://localhost:51076/");
var statusHubProxy = hubConnection.CreateHubProxy("StatusHub");
statusHubProxy.On<string>("StatusChanged", status => Console.WriteLine("New Status: {0}", status));
await hubConnection.Start();

如何调用控制器的Post方法?这是我得到例外的地方:

await statusHubProxy.Invoke("Post", "Test Status");

1 个答案:

答案 0 :(得分:0)

HubController<T>只提供一些基本的管道,使您可以访问与您要使用的特定集线器类型(例如Clients)相关联的资源。调用它与调用实际的集线器本身无关,所以你不使用集线器客户端API,它只是直接的HTTP调用。如果没有HubController<T>,您必须自己联系SignalR的GlobalHost.Configuration.GetHubContext<T>(),以便为您的集线器类型找到IHubContext

因此,您可以使用任何标准.NET HTTP API调用StatusController::Post方法:HttpClientWebClientHttpWebRequest