首页 文章

客户端未收到SignalR消息

提问于
浏览
5

我已经尝试了很多东西,现在已经采取了行动,希望能够发挥作用 . 我让我的WPF OnSignalRMessage()方法被调用 . Where am I going wrong here?

我的中心:

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

我的启动课:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HubConfiguration { EnableCrossDomain = true };

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

启动SignalR主机的方法(在我的WCF服务主机中):

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

实际发送一些消息的代码:

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

我的WPF客户端方法:

private void InitializeSignalR()
    {
        var hubConnection = new Connection("http://localhost:8084");
        hubConnection.Start();
        hubConnection.Received += OnSignalRMessage;
    }

    private void OnSignalRMessage(string data)
    {
        MessageBox.Show(data);
    }

3 回答

  • 3

    虽然我仍然在努力理解如何以及为什么,但我能够让它发挥作用 . 1到N.泰勒马伦指出我正确的方向 . 除了他在客户端的建议之外,我还必须更改一些服务器代码,即使用空集线器和简化的Startup类 .

    My hub:

    public class PrestoHub : Hub{}
    

    注意:集线器是空的,因为我们没有调用其中的方法 . 正如我们稍后将看到的,我们获得了集线器上下文并向客户端发送消息 .

    My startup class:

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

    上面的代码似乎解决了这个问题 . 这也有效:

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

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

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

    您正在创建PersistentConnection而不是集线器连接 . 要从PrestoHub获取消息,首先需要连接HubConnection,然后需要处理事件“OnSignalRMessage” .

    所以你的客户端代码现在看起来像:

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

    如果服务器端的方法是异步的,请确保它们返回任务而不是void . 那就是你应该拥有的

    public async Task Method(){ }
    

    并不是

    public async void Method(){ }
    

相关问题