asp.net-core - 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);
解决方案
推荐阅读
- react-native - 通过 react-saga 收到成功操作后,我可以更改应用程序屏幕吗?
- elasticsearch - 查询 elasticsearch 可以通过 id 获取文档,但在查询 all 中找不到
- c# - 以隐藏方式启动 IE 进程
- maven - Apache Ambari maven 构建失败:“连接到 nexus-private.hortonworks.com:80 [...] 超时”?
- c# - 使用传输语法解码数据集:不支持 JPEG 2000 图像压缩
- python - 如果您不信任全局变量,那么当您在一个方法内部时,您如何判断我们当前在哪个类中?
- node.js - 如何在 NodeJS 中传递多个自签名证书?
- java - 如何使用数组中 png 的名称以两种不同的方式对数组进行排序
- java - Java Hashmap如何处理单词网格的键冲突
- java - 卡在不会将可注入字段加载到 FXML 文档中的 javafx 程序上