首页 > 解决方案 > SignalR 在离子应用程序中 5 分钟后断开连接

问题描述

我在 ForgroundService 的离子应用程序中使用 SingalR。当应用程序打开时,信号器可以正常工作,并且在我锁定屏幕或关闭应用程序后它会继续工作大约 5 分钟,之后它会停止工作,如果我在断开连接后打开应用程序,它会重新连接并给我错误:

SignalR Disconnectundefined
SignalR DisconnectError: Error parsing handshake response: Error: Expected a handshake response from the server. 

SignalR 代码:

     public initiateConnection = () => {
        this.hubConnection = new signalR.HubConnectionBuilder()
          .withUrl(this.baseUrl + '/signalr-hubs/booking')
          .build();
        this.startConnection();
        this.onDisconnect();
      }
    
      private startConnection = () => { // Start connection 
    
        this.hubConnection
          .start()
          .then(() => {
            this.signalRConnectionStarted = true;
            console.log('Connection Started...');
            this.registerSignalEvents();
            this.getConnectionId();
            this.hubConnection.keepAliveIntervalInMilliseconds = 5000;
          })
          .catch(err => {
            console.log('Error while starting connection: ' + err);
    
            setTimeout(function() {
              this.startConnection();
            }, 3000);
          }).catch(err => {
            console.log('Connection Timeout' + err);
          });
      }

      private registerSignalEvents() { // Event listener 
    
        this.hubConnection.on('DriverClientBookingUpdate', (order: OrderModel) => {
          this.orderSvc.handleOrderUpdate(order);
        });
      }

      public onDisconnect() { // I reconnect when signalR disconnects here
        this.hubConnection.onclose((err: Error) => {
          console.log('SignalR Disconnect' + err);
          this.startConnection();
        });
      }

应用程序中的前台服务


    this.foregroundService.start(
      'Driver app is working',
      'Looking for potential orders ',
      '',
      1,
      10
    );

以及发送信号器的 C# 代码

 await_hubContext.Clients.Group(_groupName).SendAsync("DriverClientBookingUpdate", bookingUpdateEto);

启用日志记录后 更新在此处输入图像描述

标签: asp.net-coreionic-frameworksignalrionic4

解决方案


推荐阅读