简体   繁体   English

客户端未收到SignalR消息

[英]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. 我一直在尝试让我的WPF客户端应用程序接收WCF服务发送的SignalR消息。 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. 我已经在线上阅读了教程和示例,但是我根本无法调用WPF OnSignalRMessage()方法。 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): 启动SignalR主机(在WCF服务主机内)的方法:

    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: 我的WPF客户端方法:

    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. +1对N. Taylor Mullen指出正确的方向。 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. 除了他在客户端的建议外,我还必须更改一些服务器代码,即使用空集线器和简化的Startup类。

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): 但是,一旦指定了URL,我的客户端就不会收到消息(尝试使用“ SignalR”部分和不使用“ SignalR”部分):

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

The method that starts my SignalR host (within my WCF service host): 启动SignalR主机(在WCF服务主机内)的方法:

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: 我的WPF客户端方法:

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. 您正在创建PersistentConnection而不是集线器连接。 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". 为了从PrestoHub获取消息,您首先需要与HubConnection连接,然后需要处理事件“ 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. 如果服务器端的方法是异步的,请确保它们返回一个任务而不是void。 That is you should have 那是你应该有的

public async Task Method(){ }

and not 并不是

public async void Method(){ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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