locked
SignalR works slow in UWP and fast in console application RRS feed

  • Question

  • User-620501718 posted

    I have a SignalR server which sends certain messages to all the clients connected to it. I have created two sample clients one in UWP and another one is a simple console application in C#. I simply log a message once the client's method is called from SignalR.

    Dramatically the console application logs the data as soon as the server sends, but the UWP application adds a delay of about 6-30 seconds in it. Although it triggers frequently sometimes but able to reproduce the issue 6 out of 10 times.

    Any help would be highly appreciated. Thanks in advance.

    Below is the code for the UWP client, similar code is written in console application.

    async Task SetupSignalR()
        {
    
            var conn = new HubConnection(baseUrl);
            Writer.Text += (string.Format("Creating hub proxy with :{0}\n", baseUrl));
            var proxy = conn.CreateHubProxy("PumpStatusHub");
    
    
            Writer.Text += "Starting Connection\n";
    
    
            try
            {
                conn.Start().Wait();
                Writer.Text += "Connection started\n";
                proxy.Invoke("OpenPortReading").Wait();//, UserName, TextBoxMessage.Text);
                Writer.Text += "Port invoked\n";
                proxy.On<string>("ReadUdpData", OnMessage);
            }
            catch (HttpRequestException ex)
            {
                Writer.Text += "Unable to connect to server: Start server before connecting clients.\n";
                Writer.Text += ex.Message + "\n";
            }
            catch (Exception ex)
            {
                Writer.Text += ex.Message + "\n";
                Task.Delay(3000).Wait();
                SetupSignalR().Wait();
            }
        }
    
        private void OnMessage(string obj)
        {
            Windows.ApplicationModel.Core.CoreApplication.MainView.
                Dispatcher.RunAsync(CoreDispatcherPriority.High, (DispatchedHandler)(() =>
                {
                    Writer.Text += string.Format("Message received: {0}\n", counter++);
                }));
        }

    Tuesday, June 20, 2017 3:24 PM

All replies

  • User1967761114 posted

    Hi ipsit.gaur,

    According to the code which you provide, it seems like all fine, I consider might the issue is about the network environment, such as try to use IP address to instead localhost.

    You also could try to enable SignalR tracing both on server and client by the following link , and give the trace info for us to analysis this issue.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/testing-and-debugging/enabling-signalr-tracing#enabling-tracing-in-the-net-client-windows-desktop-apps

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Wednesday, June 21, 2017 6:43 AM
  • User-620501718 posted

    I have added logs on the UWP app and the Console application and following are the observations from the logs

    First Message got received at same time in both the apps, but for second message delay was observed in the UWP app.

    Console Application Logs-
    08:44:45.3689067 - cfee7a0d-7fcb-4c0e-95d7-ef8ebfc16658 - SSE: OnMessage(Data: {})
    08:44:55.3850190 - cfee7a0d-7fcb-4c0e-95d7-ef8ebfc16658 - SSE: OnMessage(Data: {})
    08:44:56.1073597 - cfee7a0d-7fcb-4c0e-95d7-ef8ebfc16658 - SSE: OnMessage(Data: .... Data

    UWP Application Logs-
    08:44:45.3821041 - 37a2a4a9-d430-4869-adb9-23ceb72ffcca - SSE: OnMessage(Data: {})
    08:44:55.3840407 - 37a2a4a9-d430-4869-adb9-23ceb72ffcca - SSE: OnMessage(Data: {})
    08:45:05.4011226 - 37a2a4a9-d430-4869-adb9-23ceb72ffcca - SSE: OnMessage(Data: ... Data

    You can clearly see the difference of 9 seconds in the UWP app logs.

    Thursday, June 22, 2017 9:37 AM
  • User1967761114 posted

    Hi ipsit.gaur,

    Try to specify the transport at start such as the following code:

    conn.Start(new Microsoft.AspNet.SignalR.Client.Transports.WebSocketTransport()).Wait();

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 10:15 AM
  • User-620501718 posted

    I am using Websockets as default but it fails to connect through Websockets and reconnects through Server Sent Events, apart from that Console Application is also using Server Sent events but it gets the response frequently. 

    Thursday, June 22, 2017 10:21 AM
  • User1967761114 posted

    Hi ipsit.gaur,

    In your case, I still suggest you to use WebSocketTransport .

    The ServerSentEvent might have delay, because the client send message per 3s.

    I suggest you could enable the WebSocket protocol on IIS, about this you could refer to the following link.

    https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-8/iis-80-websocket-protocol-support

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Friday, June 23, 2017 9:30 AM