1

I have the following method in my Hub class:

public class NotificationHub : Hub
{
    public void SendAll(string message)
    {
        if (1 == 0)
            Clients.All.broadcastMessage(message); // this should be unreachable
    }
}

Then, I (am trying) to call that method from my server-side code like so:

GlobalHost.ConnectionManager.GetHubContext<NotificationHub>()
    .Clients.All.broadcastMessage("broadcastMessage was called");

The method is called, and everything works. But, I didn't want broadcastMessage() to be called since it should have been unreachable.

I read this from the documentation:

You don't instantiate the Hub class or call its methods from your own code on the server; all that is done for you by the SignalR Hubs pipeline. SignalR creates a new instance of your Hub class each time it needs to handle a Hub operation such as when a client connects, disconnects, or makes a method call to the server.

Ref. http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server

But it doesn't look like it uses my methods at all. It just looks like it calls its own methods and ignores mine. How can I call my own methods using SignalR?

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • Why aren't you calling the "SendAll" function? Obviously running broadcastMessage won't run the logic in the SendAll function... – BradleyDotNET Apr 07 '14 at 18:38
  • Right, but there doesn't look like there's anyway to call SendAll. Or at least, intellisense doesn't pick it up. How can I call it? – user1477388 Apr 07 '14 at 18:39
  • @Jonesy The solution you posted works (which you've deleted). But is that the recommended way, or is it a hack? I don't see any such usage in the documentation, so just wondering. Thanks for your answer. – user1477388 Apr 07 '14 at 18:49
  • You're wanting to only broadcast messages that are going through your SendAll() method, and prevent access to `broadcastMessage` outside of that? – Jonesopolis Apr 07 '14 at 19:10
  • @Jonesy Pretty much - just seem the most logical way is to use these methods which I've defined as wrappers to the SignalR dynamic methods. Again, not sure if that's the intended use. just asking – user1477388 Apr 07 '14 at 19:11
  • I'm not sure exactly. You can decorate the Hub with the `[Authorize]` attribute, but I don't think that really applies to your case – Jonesopolis Apr 07 '14 at 19:19
  • @Jonesy The solution you posted worked, just as you said. However, I just wanted to know the recommended way. Unless, they are suggesting that we use methods like `Send()` and `SendAll()` from the client side and just use `GetHubContext<>()...` on the server side. – user1477388 Apr 07 '14 at 19:22
  • 1
    Yeah, that's how it's used. I was imagining a scenario where you want to restrict your team of developers to using only your Hub methods. But server side, you *have* to get the context to invoke the hub – Jonesopolis Apr 07 '14 at 19:33

1 Answers1

2

When you do this:

GlobalHost.ConnectionManager.GetHubContext<NotificationHub>()
    .Clients.All.broadcastMessage("broadcastMessage was called");

You're bypassing the method

public void SendAll(string message)

I am not sure why you'd expect the first method to be blocked. If you want your hub logic to work you have to work through the hub methods (example: public void SendAll(string message))

I like the solution presented here: https://stackoverflow.com/a/17897625/693272 if you want to call the hub method from outside.

Community
  • 1
  • 1
AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • I am really just looking for a way to call my Hub methods; what is the preferred way? I couldn't find anything in the documentation, just the way I have which, as you say, completely bypasses my methods. – user1477388 Apr 07 '14 at 18:58
  • Take a look at this: http://stackoverflow.com/questions/17896739/use-hub-methods-from-controller – AD.Net Apr 07 '14 at 19:11
  • I see what you mean; but that is not the ideal route either since it requires that I basically repeat myself in two places. So, I assume that this is not the preferred use, and I should simply use the dynamic methods as they are. – user1477388 Apr 07 '14 at 19:33