Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signalr cast IHubContext to actual Hub

I'm wondering if it's possible to Cast the result of var hub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); To my actual ChatHub class. Because GlobalHost.ConnectionManager.GetHubContext<ChatHub>() as ChatHub fails

On my ChatHub class I have a method UpdateTime():

public void SendTimeUpdate(DateTime time, string auth)
{
    Clients.All.UpdateTime(time, auth);
}

And I want to call it from my other class. Since I can't cast to ChatHub and invoke the SendUpdate I have to go:

GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.UpdateTime(time, auth);

But if I go this road, the method SendTimeUpdate isn't added in the proxy script /signalr/hubs

Is there a solution for this problem? I want to get the typed Hub instance and not call stuff directly on the Clients property of the IHubContext.

like image 879
user1613512 Avatar asked Jan 11 '14 22:01

user1613512


1 Answers

No you cannot cast the result of ....GetHubContext<.... to your hub class. Sorry :(.

The GetHubContext approach returns an IHubContext when a Hub is only an IHub.

If you'd like to centralize the logic just make a method that you can call into from your hub and from your external service.

like image 71
N. Taylor Mullen Avatar answered Sep 18 '22 12:09

N. Taylor Mullen