1

I'm using SignalR and have a Hub where I've overwritten the OnConnected method. The reason is, the server publishes data every 60 seconds. But when a new client connects, I want to publish the data immediately.

In a simplified form:

public class CustomerPublisher : Hub
{
    public void Publish(Customer customer)
    {
        Clients.All.receive(customer);
    }

    public override Task OnConnected()
    {
        return base.OnConnected();
    }
}

The OnConnected method is never called (I tested by debugging). I've read in several places (like here) that the client needs to have a connected handler, like:

$.connection.onlineHub.client.connected = function(){...}

However, I have a WPF client, so in .NET I tried adding this, on top of the event I was already handling:

hubProxy.On<object>("receive", OnCustomerReceived); // this line was already present
hubProxy.On<object>("connected", OnConnected); // I added this

This didn't help. But I do have a connection, because after some time, I start receiving data.

Is there anything I'm missing? How can I get SignalR to call the OnConnected method when my .NET client connects?

Peter
  • 13,733
  • 11
  • 75
  • 122
  • Peter this other question is not related your problem (it's for client side you are trying to notified on server side). Onconnected should work. Can you add also OnReconnected OnDisconnected and debug again. Also Can you show your code how you connected to CustomerPublisher hub. – Erkan Demirel Apr 01 '16 at 10:26
  • OnReconnected and OnDisconnected aren't called either. I'm thinking it must be due to the fact that I'm using Autofac, and I'm not doing it as in the documentation. I will rewrite my code to use Autofac as in the docs and see what that gives. Though I find it strange, as I'm clearly connected. – Peter Apr 04 '16 at 07:18

2 Answers2

1

You can try to use StateChanged event like this :

Connection.StateChanged += Connection_StateChanged;
void Connection_StateChanged(StateChange obj)
{
    if (obj.NewState == ConnectionState.Connected)
    {
          //do somethign here
    }
}

You ll receive Connecting and Connected states

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • Well, in fact I'm already doing that. Keep in mind I'm trying to know when a client connects at the server side. – Peter Mar 31 '16 at 13:20
  • Oh I see, I downloaded this light [project](https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b) and the `override Task OnConnected()` is called correctly from a wpf client. – Quentin Roger Mar 31 '16 at 13:38
  • Copy pasting your link didn't seem to work, so for those who hvae the same problem, you can find it [here](​https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b). I'm downloading it now and will check it out. – Peter Mar 31 '16 at 13:41
1

In my case, the problem was Autofac. I had set it up in my own way (due to the way I had set up WCF duplex before using SignalR). But it is important to follow the docs. Even though a connection was made, and my client received data, I never entered the OnConnected method. After following the documentation, I the method was called.

Peter
  • 13,733
  • 11
  • 75
  • 122