SignalR message not being received on the client

12,259

Solution 1

While I'm still struggling to understand the how and why, I was able to get it working. +1 to N. Taylor Mullen for pointing me in the right direction. In addition to his suggestion on the client side, I had to change some server code as well, namely using an empty hub and a simplified Startup class.

My hub:

public class PrestoHub : Hub{}

Note: The hub is empty because we're not calling methods within it. As we'll see later, we get the hub context and send messages to the clients.

My startup class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapHubs();
    }
}

The above code seems to be what fixed the problem. This also works:

var config = new HubConfiguration { EnableCrossDomain = true };
app.MapHubs(config);

But as soon as I specify a URL, my client doesn't receive the messages (tried with and without the "SignalR" part):

app.MapHubs("http://localhost:8084/SignalR", config);

The method that starts my SignalR host (within my WCF service host):

private void StartSignalRHost()
{
    const string url = "http://localhost:8084";
    WebApplication.Start<Startup>(url);
}

The code to actually send some message:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<PrestoHub>();
hubContext.Clients.All.OnSignalRMessage("snuh");

My WPF client method:

private void InitializeSignalR()
{
    var hubConnection = new HubConnection("http://localhost:8084");
    var prestoHubProxy = hubConnection.CreateHubProxy("PrestoHub");
    prestoHubProxy.On<string>("OnSignalRMessage", (data) =>
        {
            MessageBox.Show(data);
        });
    hubConnection.Start();
}

Solution 2

You're creating a PersistentConnection not a hub connection. In order to get messages from your PrestoHub you first need to connect with a HubConnection and then you need to handle the event "OnSignalRMessage".

So your client code would now look like:

private void InitializeSignalR()
{
    var hubConnection = new HubConnection("http://localhost:8084");
    var prestoHubProxy = hubConnection.CreateHubProxy("PrestoHub");

    // Bind the "OnSignalRMessage" to a function
    prestoHubProxy.On<string>("OnSignalRMessage", (data) => {
        MessageBox.Show(data);
    });

    hubConnection.Start();  
}
Share:
12,259
Bob Horn
Author by

Bob Horn

I'm a software developer that is passionate about elegant, quality code.

Updated on June 14, 2022

Comments

  • Bob Horn
    Bob Horn almost 2 years

    I've been trying to get my WPF client app to receive a SignalR message sent by the WCF service. I've tried many things and have now resorted to hacking away in the hopes that something just works. I've followed tutorials and examples online, and I simply can't get my WPF OnSignalRMessage() method to get called. Where am I going wrong here?

    My hub:

    public class PrestoHub : Hub
    {
        public void Send(string message)
        {
            Clients.All.OnSignalRMessage(message);
        }
    }
    

    My startup class:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration { EnableCrossDomain = true };
    
            app.MapHubs("http://localhost:8084", config);
        }
    }
    

    The method that starts my SignalR host (within my WCF service host):

        private void StartSignalRHost()
        {
            const string url = "http://localhost:8084";
            WebApplication.Start<Startup>(url);
        }
    

    The code to actually send some message:

    GlobalHost.ConnectionManager.GetHubContext<PrestoHub>().Clients.All.OnSignalRMessage("snuh");
    Console.WriteLine("Sent 'snuh' to all clients...");
    

    My WPF client methods:

        private void InitializeSignalR()
        {
            var hubConnection = new Connection("http://localhost:8084");
            hubConnection.Start();
            hubConnection.Received += OnSignalRMessage;
        }
    
        private void OnSignalRMessage(string data)
        {
            MessageBox.Show(data);
        }
    
  • Bob Horn
    Bob Horn almost 11 years
    Actually, using HubConnection was one of my attempts. I just tried your code example and the MessageBox.Show() isn't getting hit.
  • N. Taylor Mullen
    N. Taylor Mullen almost 11 years
    Ensure that your MapHubs is being hit, and if it is go to localhost:8084/signalr/hubs in a web browser and see if you get a javascript file.
  • Bob Horn
    Bob Horn almost 11 years
    MapHubs is being hit, but that page won't come up in the browser. I just printed the SignalR ebook. I'll read that and do some more troubleshooting. Thanks for the tips.
  • N. Taylor Mullen
    N. Taylor Mullen almost 11 years
    Reason why specifying a URL caused your application to fail: github.com/SignalR/SignalR/issues/962
  • Bob Horn
    Bob Horn almost 11 years
    So it's because I was specifying the full URL? If I want to change it, I need to do something like this? MapHubs("~/SignalR2")