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 *_statusGoEndpoint;
|
||||||
NSString *_signalEndpoint;
|
NSString *_signalEndpoint;
|
||||||
NSString *_rootDataDir;
|
NSString *_rootDataDir;
|
||||||
|
NSTimer *_pingTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
RCT_EXPORT_MODULE();
|
RCT_EXPORT_MODULE();
|
||||||
|
@ -77,6 +78,8 @@ RCT_EXPORT_MODULE();
|
||||||
|
|
||||||
[self.webSocket resume];
|
[self.webSocket resume];
|
||||||
[self receiveMessage];
|
[self receiveMessage];
|
||||||
|
|
||||||
|
[self startPingTimer];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)receiveMessage {
|
- (void)receiveMessage {
|
||||||
|
@ -109,6 +112,7 @@ RCT_EXPORT_MODULE();
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)disconnectWebSocket {
|
- (void)disconnectWebSocket {
|
||||||
|
[self stopPingTimer];
|
||||||
if (self.webSocket) {
|
if (self.webSocket) {
|
||||||
[self.webSocket cancel];
|
[self.webSocket cancel];
|
||||||
self.webSocket = nil;
|
self.webSocket = nil;
|
||||||
|
@ -119,6 +123,7 @@ RCT_EXPORT_MODULE();
|
||||||
body:(NSString *)body
|
body:(NSString *)body
|
||||||
callback:(void (^)(NSString *response, NSError *error))callback {
|
callback:(void (^)(NSString *response, NSError *error))callback {
|
||||||
NSString *fullUrlString = [NSString stringWithFormat:@"%@%@", self.statusGoEndpoint, endpoint];
|
NSString *fullUrlString = [NSString stringWithFormat:@"%@%@", self.statusGoEndpoint, endpoint];
|
||||||
|
NSLog(@"StatusGo server is enabled, executing request, endpoint: %@, body: %@", fullUrlString, body);
|
||||||
NSURL *url = [NSURL URLWithString:fullUrlString];
|
NSURL *url = [NSURL URLWithString:fullUrlString];
|
||||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
||||||
request.HTTPMethod = @"POST";
|
request.HTTPMethod = @"POST";
|
||||||
|
@ -225,7 +230,6 @@ RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)serverEnabled
|
||||||
if (client.serverEnabled) {
|
if (client.serverEnabled) {
|
||||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||||
__block NSString *resultString = @"";
|
__block NSString *resultString = @"";
|
||||||
|
|
||||||
[client request:endpoint body:body callback:^(NSString *response, NSError *error) {
|
[client request:endpoint body:body callback:^(NSString *response, NSError *error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
NSLog(@"request to %@ failed: %@", endpoint, 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
|
@end
|
||||||
|
|
Loading…
Reference in New Issue