【问题标题】:Signalr connects but doesn't push a messageSignalr 连接但不推送消息
【发布时间】:2014-07-11 18:31:33
【问题描述】:

我一直致力于将信号器作为 wcf 服务的一部分来与 .net 客户端通信。除了连接消息之外,所有通信都是将动态有效负载传递到客户端的一种方式。

我已设法设置它,以便客户端连接到服务并传递连接消息,但我无法将消息从服​​务推送到客户端。

抱歉,如果我在其他地方错过了这个答案,但我一直无法找到失败的原因,因为它似乎遵循“如何做”

任何帮助将不胜感激,并在此先感谢您

服务器端:

WCF 外部调用

public class MessageService : IMessageService
{

    public string PushAlerts()
    {
         var payLoad = new PayLoad
                      {
                          MethodName = "alerts"
                      };


            IHubContext connectionHub = GlobalHost.ConnectionManager.GetHubContext<PushConnection>();

            connectionHub.Clients.All.Notify(payLoad);
    }
}

我的中心

[HubName("PushHub")]
public class PushHub : Hub
{
    public override Task OnConnected()
    {
        var connectionMessage = Context.QueryString["CONNECTION MESSAGE"];
        if (connectionMessage != null)
        {
            Debug.WriteLine("connectionMessage");
        }
        return base.OnConnected();
    }
}

客户端:

            var querystringData = new Dictionary<string, string>{};
            querystringData.Add("CONNECTION MESSAGE", "foo Connection");

            var hubConnection = new HubConnection("http://localhost:60479/", querystringData); //Running local till working

            hubConnection.TraceLevel = TraceLevels.All;
            hubConnection.TraceWriter = Console.Out;


            IHubProxy clientHubProxy = hubConnection.CreateHubProxy("PushHub");

            clientHubProxy.On("Notify", payLoad =>

                                                    SynchronizationContext.Current.Post(delegate
                                                                                        {
                                                                                            ResponseMethod(payLoad);
                                                                                        }, null)
                );
            await hubConnection.Start();

我错过了有效载荷,但目前只包含一个字符串值。我还设置了一个用于记录 perpose 的管道模块。

再次感谢

【问题讨论】:

标签: c# asp.net .net wcf signalr


【解决方案1】:

好的,所以我通过两种方式解决了这个问题,首先我将调用移动到集线器内部的客户端,然后我从我的 wcf 服务中的一个方法调用它。

[HubName("PushHub")]
public class PushHub : Hub
{

    IHubContext connectionHub = GlobalHost.ConnectionManager.GetHubContext<PushConnection>();

public void Send(Payload payload)
{
        connectionHub.Clients.All.Notify(payLoad);

}
}

其次,该方法的客户端代码都是错误的。最后这行得通:

clientHubProxy.On("Notify", (payLoad) => { dostuff };

花了很多时间,但希望我的回答对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-12
    • 2022-01-25
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多