简体   繁体   中英

SignalR message not being received on the client

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);
    }

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();
}

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();  
}

If your methods on the server side are asynchronous make sure they return a task instead of void. That is you should have

public async Task Method(){ }

and not

public async void Method(){ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM