I'm using SingalR in ionic application with ForgroundService. While the application is open signalr works without any error and after i lock the screen or close the app it keeps working for around 5 minutes, after that it stops working, if i open the app after it disconnects it reconnects and gives me the error:
SignalR Disconnectundefined
SignalR DisconnectError: Error parsing handshake response: Error: Expected a handshake response from the server.
SignalR code:
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();
});
}
foreground service in app copmonent
this.foregroundService.start(
'Driver app is working',
'Looking for potential orders ',
'',
1,
10
);
and the c# code that sends signalr
await_hubContext.Clients.Group(_groupName).SendAsync("DriverClientBookingUpdate", bookingUpdateEto);
