mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
9ee0e1c78e
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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: https://github.com/facebook/react-native/pull/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
110 lines
2.2 KiB
Objective-C
110 lines
2.2 KiB
Objective-C
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import <React/RCTReconnectingWebSocket.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
#import <React/RCTDefines.h>
|
|
|
|
#import <SocketRocket/SRWebSocket.h>
|
|
|
|
#if RCT_DEV // Only supported in dev mode
|
|
|
|
@interface RCTReconnectingWebSocket () <SRWebSocketDelegate>
|
|
@end
|
|
|
|
@implementation RCTReconnectingWebSocket {
|
|
NSURL *_url;
|
|
SRWebSocket *_socket;
|
|
BOOL _stopped;
|
|
}
|
|
|
|
- (instancetype)initWithURL:(NSURL *)url queue:(dispatch_queue_t)queue
|
|
{
|
|
if (self = [super init]) {
|
|
_url = url;
|
|
_delegateDispatchQueue = queue;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithURL:(NSURL *)url
|
|
{
|
|
return [self initWithURL:url queue:dispatch_get_main_queue()];
|
|
}
|
|
|
|
- (void)send:(id)data
|
|
{
|
|
[_socket sendData:data error:nil];
|
|
}
|
|
|
|
- (void)start
|
|
{
|
|
[self stop];
|
|
_stopped = NO;
|
|
_socket = [[SRWebSocket alloc] initWithURL:_url];
|
|
_socket.delegate = self;
|
|
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
|
|
[_socket open];
|
|
}
|
|
|
|
- (void)stop
|
|
{
|
|
_stopped = YES;
|
|
_socket.delegate = nil;
|
|
[_socket closeWithCode:1000 reason:@"Invalidated"];
|
|
_socket = nil;
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
|
|
{
|
|
[_delegate reconnectingWebSocket:self didReceiveMessage:message];
|
|
}
|
|
|
|
- (void)reconnect
|
|
{
|
|
if (_stopped) {
|
|
return;
|
|
}
|
|
|
|
__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(), ^{
|
|
[weakSelf start];
|
|
if (!socket) {
|
|
[weakSelf reconnect];
|
|
}
|
|
});
|
|
}
|
|
|
|
- (void)webSocketDidOpen:(SRWebSocket *)webSocket
|
|
{
|
|
[_delegate reconnectingWebSocketDidOpen:self];
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
|
|
{
|
|
[_delegate reconnectingWebSocketDidClose:self];
|
|
if ([error code] != ECONNREFUSED) {
|
|
[self reconnect];
|
|
}
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket
|
|
didCloseWithCode:(NSInteger)code
|
|
reason:(NSString *)reason
|
|
wasClean:(BOOL)wasClean
|
|
{
|
|
[_delegate reconnectingWebSocketDidClose:self];
|
|
[self reconnect];
|
|
}
|
|
|
|
@end
|
|
|
|
#endif
|