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?

2
  • 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. Apr 1, 2016 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 4, 2016 at 7:18

2 Answers 2

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

3
  • 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, 2016 at 13:20
  • Oh I see, I downloaded this light project and the override Task OnConnected() is called correctly from a wpf client. Mar 31, 2016 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](​code.msdn.microsoft.com/windowsdesktop/…). I'm downloading it now and will check it out.
    – Peter
    Mar 31, 2016 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.