TheBach
TheBach

Reputation: 31

Nothing happens when calling method from within SignalR3 rc1 vnext application

In my current setup, i have an ASP.NET 5 vNext project running. I have setup SignalR Server 3.0.0-rc1-final and i am able to connect to my hub through my webinterface:

var visitorHub = $.connection.visitorsHub;

visitorHub.client.visitorEvent = function (message) {
   $("#visitorinfo").append("<li>" + message + "</li>");
};

$.connection.hub.start().done(function () {
   visitorHub.invoke("listenToEvents", "TestID");
});

So we are listening on visitorEvent from the hub and visitorEvent is called when listenToEvents is invoked.

My challenge come now, that i'm trying to notify from within the ASP.NET application. Using the build in IoC an SqlDependency is used to listen to events in the SQL server. Again this is working as intended, but when i'm trying to invoke the hub through it's HubContext nothing happens.

I have injected the IConnectionManager and able to get hold of my HubContext using:

var context = this.manager.GetHubContext<VisitorsHub>();

but when i do the following:

context.Clients.All.visitorEvent("2");

nothing happens.

I'm not sure why nothing happens and how i'm going to debug this?

My VisitorHub code is:

public class VisitorsHub : Hub
{
    public async Task ListenToEvents(string visitorId)
    {
        this.NotifyVisitorListeners();
    }

    public void NotifyVisitorListeners()
    {
        this.Clients.All.visitorEvent("Event");
    }
}

Upvotes: 3

Views: 143

Answers (1)

Jaka Konda
Jaka Konda

Reputation: 1436

You can enable client side logging via:

$.connection.hub.logging = true;
$.connection.hub.start();

or without generated proxy:

var connection = $.hubConnection();
connection.logging = true;
connection.start();

Otherwise when using invoke (method without a proxy), methods aren't renamed to camelCase, but remain as they are, in your case CamelCase.

To overcome this you can simply rename method name on either end, or add HubMethodName decorator in your backend:

[HubMethodName("listenToEvents")]
public async Task ListenToEvents(string visitorId)
{
    this.NotifyVisitorListeners();
}

Upvotes: 1

Related Questions