Skip to content

Commit

Permalink
Use SocketRocket for web socket library (#36471)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36471

The previous native web socket API, `RCTSRWebSocket`, appears to be an older version of the one provided as part of [SocketRocket](https://github.com/facebookincubator/SocketRocket). The latter has several improvements, such as the ability to respect proxy settings, which has been requested by a user of a React Native app.

Everything translates over pretty easily, and considering that SocketRocket is already a dependency of Flipper, there doesn't seem to be much additional cost to swapping out the libraries. If we wanted to make things even slimmer, it may even be possible to make the WebSocket library be optional for release builds.

## Changelog

[IOS] [CHANGED] - Use SocketRocket for web socket library

Pull Request resolved: #36347

Test Plan:
Validated the following:
* The WebSocket test page in RNTester
* Live reloading

Reviewed By: cortinico

Differential Revision: D43768835

Pulled By: javache

fbshipit-source-id: 11e1ac2700bc92991897c594622e6687339bfcbf
  • Loading branch information
Adam Gleitman authored and facebook-github-bot committed Mar 15, 2023
1 parent 7fee407 commit 9ee0e1c
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 1,932 deletions.
2 changes: 1 addition & 1 deletion BUCK
Expand Up @@ -320,7 +320,6 @@ REACT_PUBLIC_HEADERS = {
"React/RCTRootShadowView.h": RCTVIEWS_PATH + "RCTRootShadowView.h",
"React/RCTRootView.h": RCTBASE_PATH + "RCTRootView.h",
"React/RCTRootViewDelegate.h": RCTBASE_PATH + "RCTRootViewDelegate.h",
"React/RCTSRWebSocket.h": RCTLIB_PATH + "WebSocket/RCTSRWebSocket.h",
"React/RCTScrollEvent.h": RCTVIEWS_PATH + "ScrollView/RCTScrollEvent.h",
"React/RCTScrollView.h": RCTVIEWS_PATH + "ScrollView/RCTScrollView.h",
"React/RCTScrollableProtocol.h": RCTVIEWS_PATH + "ScrollView/RCTScrollableProtocol.h",
Expand Down Expand Up @@ -454,6 +453,7 @@ rn_apple_xplat_cxx_library(
],
deps = [
YOGA_CXX_TARGET,
"//fbobjc/VendorLib/SocketRocket:SocketRocket",
react_native_xplat_target("cxxreact:bridge"),
react_native_xplat_target("reactperflogger:reactperflogger"),
],
Expand Down
20 changes: 10 additions & 10 deletions Libraries/WebSocket/RCTReconnectingWebSocket.m
Expand Up @@ -10,16 +10,16 @@
#import <React/RCTConvert.h>
#import <React/RCTDefines.h>

#import <React/RCTSRWebSocket.h>
#import <SocketRocket/SRWebSocket.h>

#if RCT_DEV // Only supported in dev mode

@interface RCTReconnectingWebSocket () <RCTSRWebSocketDelegate>
@interface RCTReconnectingWebSocket () <SRWebSocketDelegate>
@end

@implementation RCTReconnectingWebSocket {
NSURL *_url;
RCTSRWebSocket *_socket;
SRWebSocket *_socket;
BOOL _stopped;
}

Expand All @@ -39,14 +39,14 @@ - (instancetype)initWithURL:(NSURL *)url

- (void)send:(id)data
{
[_socket send:data];
[_socket sendData:data error:nil];
}

- (void)start
{
[self stop];
_stopped = NO;
_socket = [[RCTSRWebSocket alloc] initWithURL:_url];
_socket = [[SRWebSocket alloc] initWithURL:_url];
_socket.delegate = self;
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
[_socket open];
Expand All @@ -60,7 +60,7 @@ - (void)stop
_socket = nil;
}

- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
[_delegate reconnectingWebSocket:self didReceiveMessage:message];
}
Expand All @@ -71,7 +71,7 @@ - (void)reconnect
return;
}

__weak RCTSRWebSocket *socket = _socket;
__weak SRWebSocket *socket = _socket;
__weak __typeof(self) weakSelf = self;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
Expand All @@ -82,20 +82,20 @@ - (void)reconnect
});
}

- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
- (void)webSocketDidOpen:(SRWebSocket *)webSocket
{
[_delegate reconnectingWebSocketDidOpen:self];
}

- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
{
[_delegate reconnectingWebSocketDidClose:self];
if ([error code] != ECONNREFUSED) {
[self reconnect];
}
}

- (void)webSocket:(RCTSRWebSocket *)webSocket
- (void)webSocket:(SRWebSocket *)webSocket
didCloseWithCode:(NSInteger)code
reason:(NSString *)reason
wasClean:(BOOL)wasClean
Expand Down
136 changes: 0 additions & 136 deletions Libraries/WebSocket/RCTSRWebSocket.h

This file was deleted.

0 comments on commit 9ee0e1c

Please sign in to comment.