Skip to content

Commit dab24b4

Browse files
bottledwalterfacebook-github-bot-3
authored andcommitted
Enable persistent socket between packager and bridge (1/N).
Reviewed By: javache Differential Revision: D2920590 fb-gh-sync-id: 120d812d1e9bcb79b186d3e41e8f7e153ca34f8b shipit-source-id: 120d812d1e9bcb79b186d3e41e8f7e153ca34f8b
1 parent 1172e64 commit dab24b4

5 files changed

Lines changed: 259 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTDefines.h"
11+
12+
#if RCT_DEV // Only supported in dev mode
13+
14+
#import "RCTWebSocketProxy.h"
15+
16+
@interface RCTWebSocketManager : NSObject <RCTWebSocketProxy>
17+
@end
18+
19+
#endif
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTDefines.h"
11+
12+
#if RCT_DEV // Only supported in dev mode
13+
14+
#import "RCTWebSocketManager.h"
15+
16+
#import "RCTConvert.h"
17+
#import "RCTLog.h"
18+
#import "RCTUtils.h"
19+
#import "RCTSRWebSocket.h"
20+
21+
#pragma mark - RCTWebSocketObserver
22+
23+
@interface RCTWebSocketObserver : NSObject <RCTSRWebSocketDelegate>
24+
25+
@property (nonatomic, strong) RCTSRWebSocket *socket;
26+
@property (nonatomic, weak) id<RCTWebSocketProxyDelegate> delegate;
27+
@property (nonatomic, strong) dispatch_semaphore_t socketOpenSemaphore;
28+
29+
- (instancetype)initWithURL:(NSURL *)url delegate:(id<RCTWebSocketProxyDelegate>)delegate;
30+
31+
@end
32+
33+
@implementation RCTWebSocketObserver
34+
35+
- (instancetype)initWithURL:(NSURL *)url delegate:(id<RCTWebSocketProxyDelegate>)delegate
36+
{
37+
if ((self = [self init])) {
38+
_socket = [[RCTSRWebSocket alloc] initWithURL:url];
39+
_socket.delegate = self;
40+
41+
_delegate = delegate;
42+
}
43+
return self;
44+
}
45+
46+
- (void)start
47+
{
48+
_socketOpenSemaphore = dispatch_semaphore_create(0);
49+
[_socket open];
50+
dispatch_semaphore_wait(_socketOpenSemaphore, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2));
51+
}
52+
53+
- (void)stop
54+
{
55+
_socket.delegate = nil;
56+
[_socket closeWithCode:1000 reason:@"Invalidated"];
57+
_socket = nil;
58+
}
59+
60+
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
61+
{
62+
if (_delegate) {
63+
NSError *error = nil;
64+
NSDictionary<NSString *, id> *msg = RCTJSONParse(message, &error);
65+
66+
if (!error) {
67+
[_delegate socketProxy:[RCTWebSocketManager sharedInstance] didReceiveMessage:msg];
68+
} else {
69+
RCTLogError(@"WebSocketManager failed to parse message with error %@\n<message>\n%@\n</message>", error, message);
70+
}
71+
}
72+
}
73+
74+
- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
75+
{
76+
dispatch_semaphore_signal(_socketOpenSemaphore);
77+
}
78+
79+
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
80+
{
81+
dispatch_semaphore_signal(_socketOpenSemaphore);
82+
dispatch_async(dispatch_get_main_queue(), ^{
83+
// Give the setUp method an opportunity to report an error first
84+
RCTLogError(@"WebSocket connection failed with error %@", error);
85+
});
86+
}
87+
88+
@end
89+
90+
#pragma mark - RCTWebSocketManager
91+
92+
@interface RCTWebSocketManager()
93+
94+
@property (nonatomic, strong) NSMutableDictionary *sockets;
95+
@property (nonatomic, strong) dispatch_queue_t queue;
96+
97+
@end
98+
99+
@implementation RCTWebSocketManager
100+
101+
+ (instancetype)sharedInstance
102+
{
103+
static RCTWebSocketManager *sharedInstance = nil;
104+
static dispatch_once_t onceToken;
105+
106+
dispatch_once(&onceToken, ^{
107+
sharedInstance = [self new];
108+
});
109+
110+
return sharedInstance;
111+
}
112+
113+
- (void)setDelegate:(id<RCTWebSocketProxyDelegate>)delegate forURL:(NSURL *)url
114+
{
115+
NSString *key = [url absoluteString];
116+
RCTWebSocketObserver *observer = _sockets[key];
117+
118+
if (observer) {
119+
if (!delegate) {
120+
[observer stop];
121+
[_sockets removeObjectForKey:key];
122+
} else {
123+
observer.delegate = delegate;
124+
}
125+
} else {
126+
RCTWebSocketObserver *newObserver = [[RCTWebSocketObserver alloc] initWithURL:url delegate:delegate];
127+
[newObserver start];
128+
_sockets[key] = newObserver;
129+
}
130+
}
131+
132+
- (instancetype)init
133+
{
134+
if ((self = [super init])) {
135+
_sockets = [NSMutableDictionary new];
136+
_queue = dispatch_queue_create("com.facebook.React.WebSocketManager", DISPATCH_QUEUE_SERIAL);
137+
}
138+
return self;
139+
}
140+
141+
@end
142+
143+
#endif

React/Base/RCTWebSocketProxy.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTDefines.h"
11+
12+
#if RCT_DEV // Only supported in dev mode
13+
14+
#import "RCTWebSocketProxyDelegate.h"
15+
16+
@protocol RCTWebSocketProxy
17+
18+
+ (instancetype)sharedInstance;
19+
20+
- (void)setDelegate:(id<RCTWebSocketProxyDelegate>)delegate forURL:(NSURL *)url;
21+
22+
- (instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
23+
24+
@end
25+
26+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTDefines.h"
11+
12+
#if RCT_DEV // Only supported in dev mode
13+
14+
@protocol RCTWebSocketProxy;
15+
16+
@protocol RCTWebSocketProxyDelegate
17+
- (void)socketProxy:(id<RCTWebSocketProxy>)sender didReceiveMessage:(NSDictionary<NSString *, id> *)message;
18+
@end
19+
20+
#endif

React/Modules/RCTDevMenu.m

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "RCTRootView.h"
2020
#import "RCTSourceCode.h"
2121
#import "RCTUtils.h"
22+
#import "RCTWebSocketProxy.h"
2223

2324
#if RCT_DEV
2425

@@ -117,7 +118,7 @@ - (void)callHandler
117118

118119
@end
119120

120-
@interface RCTDevMenu () <RCTBridgeModule, UIActionSheetDelegate, RCTInvalidating>
121+
@interface RCTDevMenu () <RCTBridgeModule, UIActionSheetDelegate, RCTInvalidating, RCTWebSocketProxyDelegate>
121122

122123
@property (nonatomic, strong) Class executorClass;
123124

@@ -194,6 +195,7 @@ - (instancetype)init
194195
// Delay setup until after Bridge init
195196
dispatch_async(dispatch_get_main_queue(), ^{
196197
[weakSelf updateSettings:_settings];
198+
[weakSelf connectPackager];
197199
});
198200

199201
#if TARGET_IPHONE_SIMULATOR
@@ -228,6 +230,54 @@ - (instancetype)init
228230
return self;
229231
}
230232

233+
- (NSURL *)packagerURL
234+
{
235+
NSString *host = [_bridge.bundleURL host];
236+
if (!host) {
237+
return nil;
238+
}
239+
240+
NSString *scheme = [_bridge.bundleURL scheme];
241+
NSNumber *port = [_bridge.bundleURL port];
242+
return [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@:%@/packager-proxy?role=client", scheme, host, port]];
243+
}
244+
245+
// TODO: Move non-UI logic into separate RCTDevSettings module
246+
- (void)connectPackager
247+
{
248+
Class webSocketManagerClass = NSClassFromString(@"RCTWebSocketManager");
249+
id<RCTWebSocketProxy> webSocketManager = (id <RCTWebSocketProxy>)[webSocketManagerClass sharedInstance];
250+
NSURL *url = [self packagerURL];
251+
if (url) {
252+
[webSocketManager setDelegate:self forURL:url];
253+
}
254+
}
255+
256+
- (BOOL)isSupportedVersion:(NSNumber *)version
257+
{
258+
NSArray<NSNumber *> *const kSupportedVersions = @[ @1 ];
259+
return [kSupportedVersions containsObject:version];
260+
}
261+
262+
- (void)socketProxy:(id<RCTWebSocketProxy>)sender didReceiveMessage:(NSDictionary<NSString *, id> *)message
263+
{
264+
if ([self isSupportedVersion:message[@"version"]]) {
265+
[self processTarget:message[@"target"] action:message[@"action"] options:message[@"options"]];
266+
}
267+
}
268+
269+
- (void)processTarget:(NSString *)target action:(NSString *)action options:(NSDictionary<NSString *, id> *)options
270+
{
271+
if ([target isEqualToString:@"bridge"]) {
272+
if ([action isEqualToString:@"reload"]) {
273+
if ([options[@"debug"] boolValue]) {
274+
_bridge.executorClass = NSClassFromString(@"RCTWebSocketExecutor");
275+
}
276+
[_bridge reload];
277+
}
278+
}
279+
}
280+
231281
- (dispatch_queue_t)methodQueue
232282
{
233283
return dispatch_get_main_queue();

0 commit comments

Comments
 (0)