Hubproxy。经过很长一段时间没有开火

时间:2018-05-31 17:03:30

标签: c# wpf signalr

我有一个使用SignalR HubConnection的wpf应用程序。一切都按预期工作。但是很长一段时间(例如10个小时)hubproxy.on方法没有触发,服务器正确地向客户端发送数据也是hubproxy.Invoke工作正常,只有hubproxy.On无法正常工作。没有连接错误或断开状态。它发生在生产环境

  

这是我的客户代码

 public partial class MainWindow : Window
        {
            private IHubProxy hubProxy;
            private HubConnection connection;
            private string url = "http://localhost:8080/signalr";

        public MainWindow()
        {
            InitializeConnection();
            InitializeComponent();
        }

        private void InitializeConnection()
        {
            connection = new HubConnection(url);
            hubProxy = connection.CreateHubProxy("SignalRHub");
            connection.StateChanged += connection_StateChanged;
            hubProxy.On<string>("OnMessage", OnMMessage);
            connection.Start();
        }

        void connection_StateChanged(StateChange obj)
        {
            if (obj.NewState == ConnectionState.Disconnected)
            {
                InitializeConnection();
            }
        }

        private void OnMMessage(string message)
        {
            //This not works sometimes
            MessageBox.Show(message);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //this works always, is being logged on the server
            hubProxy.Invoke("SendMessage2", new object[1] { "dfdfdf" });
        }
    }
  

这是服务器端

public class SignalRHub : Hub
{
    public Logger logger = new Logger();
    public void Subscribe()
    {
        SubscriberCache.Subscribers.Add(Context.ConnectionId);
    }

    public void UnSubscribe()
    {
        if (SubscriberCache.Subscribers.Contains(Context.ConnectionId))
            SubscriberCache.Subscribers.Remove(Context.ConnectionId);
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        UnSubscribe();
        return base.OnDisconnected(stopCalled);
    }

    public override Task OnReconnected()
    {
        if (!SubscriberCache.Subscribers.Contains(Context.ConnectionId))
            SubscriberCache.Subscribers.Add(Context.ConnectionId);

        return base.OnReconnected();
    }

    public void SendMessage(string message)
    {
        //this works always
        logger.Info("message sending to client");
        Clients.All.OnMessage(message);

    }
}

0 个答案:

没有答案