Ken Smith
Ken Smith

Reputation: 20445

Getting the right syntax for SignalR server to call client

I'm putting together a very basic sort of "hello world" app with SignalR, with the minor caveat that it's self-hosted, which introduces an additional wrinkle or two. Basically, I'm trying to figure out the right way to call methods on my client(s) from the server.

On my client, for instance, I've got a method that looks like this:

    roomHub.onEcho = function (msg) {
        console.log("onEcho called: " + msg);
    };

And I can call it successfully from my server-side hub like so:

public class RoomHub : Hub
{
    public void Echo(string echo)
    {
        Clients.onEcho(echo);
    }
}

And it works, but of course, it calls all the clients, not just one. And in the various samples I've seen online (e.g., https://github.com/SignalR/SignalR/blob/master/samples/Microsoft.AspNet.SignalR.Hosting.AspNet.Samples/Hubs/Benchmark/HubBench.cs, I see all sorts of commands that make it look like I should be able to specify who gets called, e.g.:

public void Echo(string echo)
{
    Clients.Caller.onEcho(echo);
    Clients.Caller(Context.ConnectionId).onEcho(echo);
    Clients.All.onEcho(echo);
}

But I can't get any of the above syntaxes to work. For Clients.All.onEcho() and Clients.Caller.onEcho(), absolutely nothing happens. For Clients.Caller(Context.ConnectionId).onEcho(), Firebug tells me that it's actually trying to call a Caller() method on my JavaScript roomHub instance, which of course isn't there.

Here's the weird bit, though. If I look at the Hub class, I can see why none of these work - because the Hub constructor overrides a bunch of the properties of its "Clients" object with NullClientProxies:

protected Hub()
{
    Clients = new HubConnectionContext();
    Clients.All = new NullClientProxy();
    Clients.Others = new NullClientProxy();
    Clients.Caller = new NullClientProxy();
}

But I'm kinda mystified as to why it does that - or why the samples seem to work anyway - or what the expected approach should be.

Any thoughts? What am I doing wrong here?

Upvotes: 0

Views: 2412

Answers (1)

davidfowl
davidfowl

Reputation: 38885

We've been updating docs recently so you've probably seen lots of inconsistent data around the place. The latest version of SignalR is 1.0 alpha2 ( http://weblogs.asp.net/davidfowler/archive/2012/11/11/microsoft-asp-net-signalr.aspx ). All of the documentation has been updated to show the new syntax so if you're using an older version, please upgrade. Check out the wiki for examples https://github.com/SignalR/SignalR/wiki/Hubs

Upvotes: 1

Related Questions