diff --git a/Libraries/WebSocket/RCTSRWebSocket.h b/Libraries/WebSocket/RCTSRWebSocket.h index 1b17cffaf47c08..b3e970f0a33a24 100644 --- a/Libraries/WebSocket/RCTSRWebSocket.h +++ b/Libraries/WebSocket/RCTSRWebSocket.h @@ -80,6 +80,8 @@ extern NSString *const RCTSRHTTPResponseErrorKey; - (void)open; - (void)close; +- (void)flush; + - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason; // Send a UTF8 String or Data. diff --git a/Libraries/WebSocket/RCTSRWebSocket.m b/Libraries/WebSocket/RCTSRWebSocket.m index 9e085ac7ce1836..f966053bb2d973 100644 --- a/Libraries/WebSocket/RCTSRWebSocket.m +++ b/Libraries/WebSocket/RCTSRWebSocket.m @@ -549,6 +549,13 @@ - (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode [_scheduledRunloops removeObject:@[aRunLoop, mode]]; } +- (void)flush +{ + // By queueing an empty block, all blocks queued before + // need to finish executing as this is a serial queue + dispatch_sync(_workQueue, ^{}); +} + - (void)close { [self closeWithCode:RCTSRStatusCodeNormal reason:nil]; diff --git a/React/CoreModules/RCTWebSocketModule.h b/React/CoreModules/RCTWebSocketModule.h index c1a48d3d31b9a3..6744bea36eb194 100644 --- a/React/CoreModules/RCTWebSocketModule.h +++ b/React/CoreModules/RCTWebSocketModule.h @@ -24,6 +24,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)sendData:(NSData *)data forSocketID:(nonnull NSNumber *)socketID; +// Blocking call that waits until there are no more remaining actions on the queue +- (void)flush; + @end @interface RCTBridge (RCTWebSocketModule) diff --git a/React/CoreModules/RCTWebSocketModule.mm b/React/CoreModules/RCTWebSocketModule.mm index 996ee8c83e12bc..7d66def5e2999b 100644 --- a/React/CoreModules/RCTWebSocketModule.mm +++ b/React/CoreModules/RCTWebSocketModule.mm @@ -51,6 +51,14 @@ - (NSArray *)supportedEvents return @[ @"websocketMessage", @"websocketOpen", @"websocketFailed", @"websocketClosed" ]; } + +- (void)flush +{ + for (RCTSRWebSocket *socket in _sockets.allValues) { + [socket flush]; + } +} + - (void)invalidate { [super invalidate];