Enable persistent socket between packager and bridge (1/N).

Reviewed By: javache

Differential Revision: D2920590

fb-gh-sync-id: 120d812d1e9bcb79b186d3e41e8f7e153ca34f8b
shipit-source-id: 120d812d1e9bcb79b186d3e41e8f7e153ca34f8b
This commit is contained in:
Walter Luh 2016-02-16 23:04:18 -08:00 committed by facebook-github-bot-3
parent 1172e6478f
commit dab24b4a6c
5 changed files with 259 additions and 1 deletions

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTDefines.h"
#if RCT_DEV // Only supported in dev mode
#import "RCTWebSocketProxy.h"
@interface RCTWebSocketManager : NSObject <RCTWebSocketProxy>
@end
#endif

View File

@ -0,0 +1,143 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTDefines.h"
#if RCT_DEV // Only supported in dev mode
#import "RCTWebSocketManager.h"
#import "RCTConvert.h"
#import "RCTLog.h"
#import "RCTUtils.h"
#import "RCTSRWebSocket.h"
#pragma mark - RCTWebSocketObserver
@interface RCTWebSocketObserver : NSObject <RCTSRWebSocketDelegate>
@property (nonatomic, strong) RCTSRWebSocket *socket;
@property (nonatomic, weak) id<RCTWebSocketProxyDelegate> delegate;
@property (nonatomic, strong) dispatch_semaphore_t socketOpenSemaphore;
- (instancetype)initWithURL:(NSURL *)url delegate:(id<RCTWebSocketProxyDelegate>)delegate;
@end
@implementation RCTWebSocketObserver
- (instancetype)initWithURL:(NSURL *)url delegate:(id<RCTWebSocketProxyDelegate>)delegate
{
if ((self = [self init])) {
_socket = [[RCTSRWebSocket alloc] initWithURL:url];
_socket.delegate = self;
_delegate = delegate;
}
return self;
}
- (void)start
{
_socketOpenSemaphore = dispatch_semaphore_create(0);
[_socket open];
dispatch_semaphore_wait(_socketOpenSemaphore, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2));
}
- (void)stop
{
_socket.delegate = nil;
[_socket closeWithCode:1000 reason:@"Invalidated"];
_socket = nil;
}
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
{
if (_delegate) {
NSError *error = nil;
NSDictionary<NSString *, id> *msg = RCTJSONParse(message, &error);
if (!error) {
[_delegate socketProxy:[RCTWebSocketManager sharedInstance] didReceiveMessage:msg];
} else {
RCTLogError(@"WebSocketManager failed to parse message with error %@\n<message>\n%@\n</message>", error, message);
}
}
}
- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
{
dispatch_semaphore_signal(_socketOpenSemaphore);
}
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
{
dispatch_semaphore_signal(_socketOpenSemaphore);
dispatch_async(dispatch_get_main_queue(), ^{
// Give the setUp method an opportunity to report an error first
RCTLogError(@"WebSocket connection failed with error %@", error);
});
}
@end
#pragma mark - RCTWebSocketManager
@interface RCTWebSocketManager()
@property (nonatomic, strong) NSMutableDictionary *sockets;
@property (nonatomic, strong) dispatch_queue_t queue;
@end
@implementation RCTWebSocketManager
+ (instancetype)sharedInstance
{
static RCTWebSocketManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self new];
});
return sharedInstance;
}
- (void)setDelegate:(id<RCTWebSocketProxyDelegate>)delegate forURL:(NSURL *)url
{
NSString *key = [url absoluteString];
RCTWebSocketObserver *observer = _sockets[key];
if (observer) {
if (!delegate) {
[observer stop];
[_sockets removeObjectForKey:key];
} else {
observer.delegate = delegate;
}
} else {
RCTWebSocketObserver *newObserver = [[RCTWebSocketObserver alloc] initWithURL:url delegate:delegate];
[newObserver start];
_sockets[key] = newObserver;
}
}
- (instancetype)init
{
if ((self = [super init])) {
_sockets = [NSMutableDictionary new];
_queue = dispatch_queue_create("com.facebook.React.WebSocketManager", DISPATCH_QUEUE_SERIAL);
}
return self;
}
@end
#endif

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTDefines.h"
#if RCT_DEV // Only supported in dev mode
#import "RCTWebSocketProxyDelegate.h"
@protocol RCTWebSocketProxy
+ (instancetype)sharedInstance;
- (void)setDelegate:(id<RCTWebSocketProxyDelegate>)delegate forURL:(NSURL *)url;
- (instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
@end
#endif

View File

@ -0,0 +1,20 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTDefines.h"
#if RCT_DEV // Only supported in dev mode
@protocol RCTWebSocketProxy;
@protocol RCTWebSocketProxyDelegate
- (void)socketProxy:(id<RCTWebSocketProxy>)sender didReceiveMessage:(NSDictionary<NSString *, id> *)message;
@end
#endif

View File

@ -19,6 +19,7 @@
#import "RCTRootView.h" #import "RCTRootView.h"
#import "RCTSourceCode.h" #import "RCTSourceCode.h"
#import "RCTUtils.h" #import "RCTUtils.h"
#import "RCTWebSocketProxy.h"
#if RCT_DEV #if RCT_DEV
@ -117,7 +118,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
@end @end
@interface RCTDevMenu () <RCTBridgeModule, UIActionSheetDelegate, RCTInvalidating> @interface RCTDevMenu () <RCTBridgeModule, UIActionSheetDelegate, RCTInvalidating, RCTWebSocketProxyDelegate>
@property (nonatomic, strong) Class executorClass; @property (nonatomic, strong) Class executorClass;
@ -194,6 +195,7 @@ RCT_EXPORT_MODULE()
// Delay setup until after Bridge init // Delay setup until after Bridge init
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf updateSettings:_settings]; [weakSelf updateSettings:_settings];
[weakSelf connectPackager];
}); });
#if TARGET_IPHONE_SIMULATOR #if TARGET_IPHONE_SIMULATOR
@ -228,6 +230,54 @@ RCT_EXPORT_MODULE()
return self; return self;
} }
- (NSURL *)packagerURL
{
NSString *host = [_bridge.bundleURL host];
if (!host) {
return nil;
}
NSString *scheme = [_bridge.bundleURL scheme];
NSNumber *port = [_bridge.bundleURL port];
return [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@:%@/packager-proxy?role=client", scheme, host, port]];
}
// TODO: Move non-UI logic into separate RCTDevSettings module
- (void)connectPackager
{
Class webSocketManagerClass = NSClassFromString(@"RCTWebSocketManager");
id<RCTWebSocketProxy> webSocketManager = (id <RCTWebSocketProxy>)[webSocketManagerClass sharedInstance];
NSURL *url = [self packagerURL];
if (url) {
[webSocketManager setDelegate:self forURL:url];
}
}
- (BOOL)isSupportedVersion:(NSNumber *)version
{
NSArray<NSNumber *> *const kSupportedVersions = @[ @1 ];
return [kSupportedVersions containsObject:version];
}
- (void)socketProxy:(id<RCTWebSocketProxy>)sender didReceiveMessage:(NSDictionary<NSString *, id> *)message
{
if ([self isSupportedVersion:message[@"version"]]) {
[self processTarget:message[@"target"] action:message[@"action"] options:message[@"options"]];
}
}
- (void)processTarget:(NSString *)target action:(NSString *)action options:(NSDictionary<NSString *, id> *)options
{
if ([target isEqualToString:@"bridge"]) {
if ([action isEqualToString:@"reload"]) {
if ([options[@"debug"] boolValue]) {
_bridge.executorClass = NSClassFromString(@"RCTWebSocketExecutor");
}
[_bridge reload];
}
}
}
- (dispatch_queue_t)methodQueue - (dispatch_queue_t)methodQueue
{ {
return dispatch_get_main_queue(); return dispatch_get_main_queue();