1
votes

I realize this question has been asked several times, but the solution others posted is not my solution. So, I'm hoping there's another answer to this problem that solves it for me.

When debugging this on this on the server, it finds the appropriate client and calls the "addAlert" method. This Javascript gets initialized fine and the "Alert Ready" message is written to the console.

There are no error messages on either the server nor client code:

$(function () {

    // enable logging for debugging
    $.connection.hub.logging = true;

    // Declare a proxy to reference the hub. 
    var hub = $.connection.alertHub;

    hub.client.addAlert = function (id, title, url, dateTime) {
        console.log(title);
    };

    $.connection.hub.start().done(function () {
        console.log("Alert Ready");
    });
});

[09:12:51 GMT-0600 (Central America Standard Time)] SignalR: Client subscribed to hub 'alerthub'. jquery.signalR-2.0.0.min.js:8 [09:12:51 GMT-0600 (Central America Standard Time)] SignalR: Negotiating with '/signalr/negotiate?connectionData=%5B%7B%22name%22%3A%22alerthub%22%7D%5D&clientProtocol=1.3'. jquery.signalR-2.0.0.min.js:8 [09:12:51 GMT-0600 (Central America Standard Time)] SignalR: Connecting to websocket endpoint 'ws://mysite.local/signalr/connect?transport=webSockets&connectionToken=c…OZxqiRQKOc%3D&connectionData=%5B%7B%22name%22%3A%22alerthub%22%7D%5D&tid=4'. jquery.signalR-2.0.0.min.js:8 [09:12:51 GMT-0600 (Central America Standard Time)] SignalR: Websocket opened. jquery.signalR-2.0.0.min.js:8 [09:12:51 GMT-0600 (Central America Standard Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000.

The above log is all that happens, even after the client function is called form the server:

string dateTime;
foreach(var connection in ConnectionMapping<Guid>.GetConnections(userId))
{
    dateTime = SystemTime.Now().ToString();
    _alertHubContext.Clients.Client(connection).addAlert(alert.Id.Shrink(), title, url, dateTime);
}

The other solutions all describe a situation where the developer was calling start() before wiring up client methods. However, I am not doing that.

When the server method calls the client method, the console does not change at all.

I believe I've narrowed it down to this:

Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<AlertHub>().Clients.All.hi();

I created a basic client method called hi() which just pops an alert. This isn't even working.

It seems that the connection is not being made to the client code when using the GetHubContext method to call the client methods.

1

1 Answers

1
votes

As it turns out, my client functions were not being called because I was using a custom dependency resolver. Apparently if you set your DependencyResolver in the OwinStartup class, it somehow prevents the client methods from being called.

So, moving it to the Global.asax, or more specifically the Application_Start event, solves the problem:

protected void Application_Start()
{
    GlobalHost.DependencyResolver = new SignalrDefaultDependencyResolver(UnityConfig.Container);
}