feat(websocket): add ping/pong keep-alive mechanism for iOS websocket (#21733)
Add WebSocket keep-alive mechanism using ping/pong frames to maintain connection stability: - Add ping timer that sends ping every 30 seconds - Automatically reconnect if ping fails - Properly cleanup timer on disconnect - Add logging for StatusGo server requests This helps prevent connection timeouts and ensures timely detection of connection issues.
This commit is contained in:
parent
1391c48e39
commit
e60c20bc10
|
@ -8,6 +8,7 @@
|
|||
NSString *_statusGoEndpoint;
|
||||
NSString *_signalEndpoint;
|
||||
NSString *_rootDataDir;
|
||||
NSTimer *_pingTimer;
|
||||
}
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
@ -77,6 +78,8 @@ RCT_EXPORT_MODULE();
|
|||
|
||||
[self.webSocket resume];
|
||||
[self receiveMessage];
|
||||
|
||||
[self startPingTimer];
|
||||
}
|
||||
|
||||
- (void)receiveMessage {
|
||||
|
@ -109,6 +112,7 @@ RCT_EXPORT_MODULE();
|
|||
}
|
||||
|
||||
- (void)disconnectWebSocket {
|
||||
[self stopPingTimer];
|
||||
if (self.webSocket) {
|
||||
[self.webSocket cancel];
|
||||
self.webSocket = nil;
|
||||
|
@ -119,6 +123,7 @@ RCT_EXPORT_MODULE();
|
|||
body:(NSString *)body
|
||||
callback:(void (^)(NSString *response, NSError *error))callback {
|
||||
NSString *fullUrlString = [NSString stringWithFormat:@"%@%@", self.statusGoEndpoint, endpoint];
|
||||
NSLog(@"StatusGo server is enabled, executing request, endpoint: %@, body: %@", fullUrlString, body);
|
||||
NSURL *url = [NSURL URLWithString:fullUrlString];
|
||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
||||
request.HTTPMethod = @"POST";
|
||||
|
@ -225,7 +230,6 @@ RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)serverEnabled
|
|||
if (client.serverEnabled) {
|
||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||
__block NSString *resultString = @"";
|
||||
|
||||
[client request:endpoint body:body callback:^(NSString *response, NSError *error) {
|
||||
if (error) {
|
||||
NSLog(@"request to %@ failed: %@", endpoint, error);
|
||||
|
@ -243,4 +247,33 @@ RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)serverEnabled
|
|||
}
|
||||
}
|
||||
|
||||
- (void)startPingTimer {
|
||||
[self stopPingTimer];
|
||||
_pingTimer = [NSTimer scheduledTimerWithTimeInterval:30.0
|
||||
target:self
|
||||
selector:@selector(sendPing)
|
||||
userInfo:nil
|
||||
repeats:YES];
|
||||
}
|
||||
|
||||
- (void)stopPingTimer {
|
||||
if (_pingTimer) {
|
||||
[_pingTimer invalidate];
|
||||
_pingTimer = nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)sendPing {
|
||||
if (self.webSocket) {
|
||||
[self.webSocket sendPingWithPongReceiveHandler:^(NSError * _Nullable error) {
|
||||
if (error) {
|
||||
NSLog(@"WebSocket ping failed: %@", error);
|
||||
// Reconnect on ping failure
|
||||
[self disconnectWebSocket];
|
||||
[self connectWebSocket];
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue