Skip to content

Commit

Permalink
feat(ios): websocket url add hash and close with code and reason (#1447)
Browse files Browse the repository at this point in the history
* feat(ios): websocket close with code and reason

* feat(hippy-vue-demo): add remote bundle entry

* feat(vue-demo): add TestModule.remoteDebug API

* feat(ios): websocket url add hash

* feat(ios): TestModule add remoteDebug

Co-authored-by: chestershen <chestershen@tencent.com>
  • Loading branch information
2 people authored and ozonelmy committed Jan 17, 2022
1 parent ac7590f commit 0aea441
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<template>
<div
ref="inputDemo"
class="demo-input"
>
<label class="input-label">BundleURL:</label>
<input
ref="input"
v-model="text"
caret-color="yellow"
placeholder="Text"
class="input"
@click="stopPropagation"
@keyboardWillShow="onKeyboardWillShow"
>
<button
class="input-button"
@click="openBundle"
>
<span>打开 bundle</span>
</button>
</div>
</template>

<script>
import Vue from 'vue';
/**
* 这个 Demo 里有直接操作 DOM 的章节
*/
export default {
/**
* 组件加载时自动 focus 第一个输入框
*/
data() {
return {
text: '',
};
},
methods: {
openBundle() {
Vue.Native.callNative('TestModule', 'remoteDebug', this.$root.$options.rootViewId, this.text);
},
/**
* 点击输入框时,点击事件会冒泡到顶部 View 导致 focus 时又被 blur 了,所以这里需要阻止一下冒泡
*/
stopPropagation(evt) {
evt.stopPropagation();
},
clearTextContent() {
this.text = '';
},
getChildNodes(childNodes) {
return !Vue.Native ? Array.from(childNodes) : childNodes;
},
},
};
</script>

<style scoped>
.demo-input {
display: flex;
flex: 1;
align-items: flex-start;
flex-direction: column;
}
.input-label {
margin: 20px;
margin-bottom: 0;
}
.demo-input .input {
width: 300px;
height: 48px;
color: #242424;
border-width: 1px;
border-color: #ccc;
font-size: 16px;
margin: 20px;
placeholder-text-color: #aaa;
/* underline-color-android: #40b883; */
}
.demo-input .input-button {
border-color: #4c9afa;
border-width: 1px;
padding-left: 10px;
padding-right: 10px;
margin-top: 5px;
margin-bottom: 5px;
margin-left: 20px;
margin-right: 20px;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import demoIFrame from './demo-iframe.vue';
import demoWebSocket from './demo-websocket.vue';
import demoDynamicImport from './demo-dynamicimport.vue';
import demoTurbo from './demo-turbo.vue';
import demoRemoteBundle from './demo-remote-bundle.vue';

const demos = {
demoDiv: {
Expand Down Expand Up @@ -60,6 +61,10 @@ const demos = {
name: 'Turbo',
component: demoTurbo,
},
demoRemoteBundle: {
name: 'RemoteBundle',
component: demoRemoteBundle,
},
};

export default demos;
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
06FF8BD62511BC0400C03900 /* HippyImageProviderProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = 06FF8BD52511BC0400C03900 /* HippyImageProviderProtocol.m */; };
06FF8EEC2511C10B00C03900 /* HippyDefaultImageProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 06FF8EEB2511C10B00C03900 /* HippyDefaultImageProvider.m */; };
06FF8EEF2511C20900C03900 /* NSData+DataType.m in Sources */ = {isa = PBXBuildFile; fileRef = 06FF8EEE2511C20900C03900 /* NSData+DataType.m */; };
781580042795449A006E92D4 /* HippyDevInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 7815800327954499006E92D4 /* HippyDevInfo.m */; };
787C199C2726A1020062EBFF /* HippyPageModel.mm in Sources */ = {isa = PBXBuildFile; fileRef = 787C198F2726A1020062EBFF /* HippyPageModel.mm */; };
787C199D2726A1020062EBFF /* HippyCSSModel.mm in Sources */ = {isa = PBXBuildFile; fileRef = 787C19902726A1020062EBFF /* HippyCSSModel.mm */; };
787C199E2726A1020062EBFF /* HippyDomModel.mm in Sources */ = {isa = PBXBuildFile; fileRef = 787C19912726A1020062EBFF /* HippyDomModel.mm */; };
Expand Down Expand Up @@ -498,6 +499,8 @@
06FF8EEB2511C10B00C03900 /* HippyDefaultImageProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HippyDefaultImageProvider.m; sourceTree = "<group>"; };
06FF8EED2511C20900C03900 /* NSData+DataType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+DataType.h"; sourceTree = "<group>"; };
06FF8EEE2511C20900C03900 /* NSData+DataType.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+DataType.m"; sourceTree = "<group>"; };
7815800227954499006E92D4 /* HippyDevInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HippyDevInfo.h; sourceTree = "<group>"; };
7815800327954499006E92D4 /* HippyDevInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HippyDevInfo.m; sourceTree = "<group>"; };
787C198A2726A1020062EBFF /* HippyInspector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HippyInspector.h; sourceTree = "<group>"; };
787C198C2726A1020062EBFF /* HippyCSSModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HippyCSSModel.h; sourceTree = "<group>"; };
787C198D2726A1020062EBFF /* HippyPageModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HippyPageModel.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1753,6 +1756,8 @@
F4E2AD78271FC23500E36EFB /* HippyDevWebSocketClient.m */,
F417B54B2727ACCE00894090 /* HippyDevManager.h */,
F417B54C2727ACCE00894090 /* HippyDevManager.mm */,
7815800227954499006E92D4 /* HippyDevInfo.h */,
7815800327954499006E92D4 /* HippyDevInfo.m */,
);
path = devtools;
sourceTree = "<group>";
Expand Down Expand Up @@ -1935,6 +1940,7 @@
064C5A2923AB1A51001E80DD /* HippyBorderDrawing.m in Sources */,
064C5A3523AB1A51001E80DD /* HippyModalCustomPresentationController.m in Sources */,
064C5A2B23AB1A51001E80DD /* UIView+Hippy.mm in Sources */,
781580042795449A006E92D4 /* HippyDevInfo.m in Sources */,
064C5A4A23AB1A51001E80DD /* HippyModuleMethod.mm in Sources */,
D84AB9C6273A618B00987D03 /* thread.cc in Sources */,
06FF8BD62511BC0400C03900 /* HippyImageProviderProtocol.m in Sources */,
Expand Down
39 changes: 39 additions & 0 deletions framework/js/examples/ios-demo/HippyDemo/TestModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
#import "TestModule.h"
#import "HippyRootView.h"
#import "AppDelegate.h"
#import "HippyBundleURLProvider.h"
#import "DemoConfigs.h"

@interface TestModule ()<HippyBridgeDelegate>

@end

@implementation TestModule

Expand Down Expand Up @@ -60,4 +66,37 @@ - (dispatch_queue_t)methodQueue
[nav presentViewController:vc animated:YES completion:NULL];
}

HIPPY_EXPORT_METHOD(remoteDebug:(nonnull NSNumber *)instanceId bundleUrl:(nonnull NSString *)bundleUrl)
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *nav = delegate.window.rootViewController;
UIViewController *vc = [[UIViewController alloc] init];
BOOL isSimulator = NO;
#if TARGET_IPHONE_SIMULATOR
isSimulator = YES;
#endif
NSString *urlString = [[HippyBundleURLProvider sharedInstance] bundleURLString];
if (bundleUrl.length > 0) {
urlString = bundleUrl;
}

NSURL *url = [NSURL URLWithString:urlString];
NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(YES)};
HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self bundleURL:url moduleProvider:nil launchOptions:launchOptions executorKey:@"Demo"];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge moduleName:@"Demo" initialProperties:@{@"isSimulator": @(isSimulator)} shareOptions:nil delegate:nil];
rootView.backgroundColor = [UIColor whiteColor];
rootView.frame = vc.view.bounds;
[vc.view addSubview:rootView];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[nav presentViewController:vc animated:YES completion:NULL];
}

- (BOOL)shouldStartInspector:(HippyBridge *)bridge {
return bridge.debugMode;
}

- (NSURL *)inspectorSourceURLForBridge:(HippyBridge *)bridge {
return bridge.bundleURL;
}

@end
3 changes: 1 addition & 2 deletions framework/js/examples/ios-demo/HippyDemo/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ - (void)runCommonDemo {

#ifdef HIPPYDEBUG
NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(YES)};
NSString *localhost = [HippyBundleURLProvider sharedInstance].localhost ?: @"localhost:38989";
NSString *bundleStr = [NSString stringWithFormat:@"http://%@%@", localhost, [HippyBundleURLProvider sharedInstance].debugPathUrl];
NSString *bundleStr = [HippyBundleURLProvider sharedInstance].bundleURLString;
NSURL *bundleUrl = [NSURL URLWithString:bundleStr];
HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self
bundleURL:bundleUrl
Expand Down
23 changes: 15 additions & 8 deletions ios/sdk/base/HippyBatchedBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -489,19 +489,22 @@ - (void)setUpExecutor {
- (void)setUpDevClientWithName:(NSString *)name {
if ([self.delegate respondsToSelector:@selector(shouldStartInspector:)]) {
if ([self.delegate shouldStartInspector:self.parentBridge]) {
NSString *ipAddress = nil;
NSString *ipPort = nil;
HippyDevInfo *devInfo = [[HippyDevInfo alloc] init];
if ([self.delegate respondsToSelector:@selector(inspectorSourceURLForBridge:)]) {
NSURL *url = [self.delegate inspectorSourceURLForBridge:self.parentBridge];
ipAddress = [url host];
ipPort = [NSString stringWithFormat:@"%@", [url port]];
devInfo.scheme = [url scheme];
devInfo.ipAddress = [url host];
devInfo.port = [NSString stringWithFormat:@"%@", [url port]];
devInfo.versionId = [HippyBundleURLProvider parseVersionId:[url path]];
}
else {
HippyBundleURLProvider *bundleURLProvider = [HippyBundleURLProvider sharedInstance];
ipAddress = bundleURLProvider.localhostIP;
ipPort = bundleURLProvider.localhostPort;
devInfo.scheme = bundleURLProvider.scheme;
devInfo.ipAddress = bundleURLProvider.localhostIP;
devInfo.port = bundleURLProvider.localhostPort;
devInfo.versionId = bundleURLProvider.versionId;
}
_devManager = [[HippyDevManager alloc] initWithBridge:self.parentBridge devIPAddress:ipAddress devPort:ipPort contextName:name];
_devManager = [[HippyDevManager alloc] initWithBridge:self.parentBridge devInfo:devInfo contextName:name];
}
}
}
Expand Down Expand Up @@ -784,7 +787,11 @@ - (void)invalidate {
[HippyBridge setCurrentBridge:nil];
}
if (_devManager) {
[_devManager closeWebSocket];
HippyDevCloseType closeType = HippyDevCloseTypeClosePage;
if (self.invalidateReason == HippyInvalidateReasonReload) {
closeType = HippyDevCloseTypeReload;
}
[_devManager closeWebSocket:closeType];
}

// Invalidate modules
Expand Down
2 changes: 2 additions & 0 deletions ios/sdk/base/HippyBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);

@property (nonatomic, strong) NSString *appVerson; //

@property (nonatomic, assign) HippyInvalidateReason invalidateReason;

/**
* just for debugger
*/
Expand Down
5 changes: 5 additions & 0 deletions ios/sdk/base/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ - (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
_shareOptions = [NSMutableDictionary new];
_appVerson = @"";
_executorKey = executorKey;
_invalidateReason = HippyInvalidateReasonDealloc;
[self setUp];

HippyExecuteOnMainQueue(^{
Expand Down Expand Up @@ -228,6 +229,8 @@ - (void)dealloc {
*/
HippyLogInfo(@"[Hippy_OC_Log][Life_Circle],%@ dealloc %p", NSStringFromClass([self class]), self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.invalidateReason = HippyInvalidateReasonDealloc;
self.batchedBridge.invalidateReason = HippyInvalidateReasonDealloc;
[self invalidate];
}

Expand Down Expand Up @@ -322,6 +325,8 @@ - (void)reload {
* Any thread
*/
dispatch_async(dispatch_get_main_queue(), ^{
self.invalidateReason = HippyInvalidateReasonReload;
self.batchedBridge.invalidateReason = HippyInvalidateReasonReload;
[self invalidate];
[self setUp];
});
Expand Down
10 changes: 10 additions & 0 deletions ios/sdk/base/HippyBundleURLProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
@property (nonatomic, copy, readonly) NSString *localhostIP;
@property (nonatomic, copy, readonly) NSString *localhostPort;
@property (nonatomic, copy, readonly) NSString *debugPathUrl;
@property (nonatomic, copy, readonly) NSString *versionId;
@property (nonatomic, copy, readonly) NSString *scheme;

/**
* @return instancetype
*/
+ (instancetype)sharedInstance;

+ (NSString *)parseVersionId:(NSString *)path;

/**
* set local debug ip & port
* default is localhost:38989
Expand All @@ -41,10 +45,16 @@
*/
- (void)setLocalhostIP:(NSString *)localhostIP localhostPort:(NSString *)localhostPort;

- (void)setScheme:(NSString *)scheme;

- (void)setDebugPathUrl:(NSString *)debugPathUrl;

- (NSString *)localhost;

- (NSString *)debugPathUrl;

- (NSString *)versionId;

- (NSString *)bundleURLString;

@end
34 changes: 33 additions & 1 deletion ios/sdk/base/HippyBundleURLProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@

#import "HippyBundleURLProvider.h"

NSString *const HippyBundleURLSchemeHttp = @"http";
NSString *const HippyBundleURLSchemetHttps = @"https";

@interface HippyBundleURLProvider ()

@property (nonatomic, copy) NSString *localhostIP;
@property (nonatomic, copy) NSString *localhostPort;
@property (nonatomic, copy) NSString *debugPathUrl;
@property (nonatomic, copy) NSString *versionId;
@property (nonatomic, copy) NSString *scheme;

@end

Expand All @@ -43,12 +48,27 @@ + (instancetype)sharedInstance {
return sharedInstance;
}

+ (NSString *)parseVersionId:(NSString *)path {
if (path.length <= 0) {
return @"";
}
if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1];
}
NSArray<NSString *> *pathArray = [path componentsSeparatedByString:@"/"];
if (pathArray.count <= 1) {
return @"";
}
return [pathArray firstObject];
}

- (instancetype)init {
self = [super init];
if (self) {
_scheme = HippyBundleURLSchemeHttp;
_localhostIP = @"localhost";
_localhostPort = @"38989";
_debugPathUrl = @"/index.bundle?platform=ios&dev=true&minify=false";
self.debugPathUrl = @"/index.bundle?platform=ios&dev=true&minify=false";
}
return self;
}
Expand All @@ -64,14 +84,26 @@ - (void)setLocalhostIP:(NSString *)localhostIP localhostPort:(NSString *)localho
}
}

- (void)setScheme:(NSString *)scheme {
if (scheme) {
_scheme = scheme;
}
}

- (void)setDebugPathUrl:(NSString *)debugPathUrl {
if (debugPathUrl) {
_debugPathUrl = debugPathUrl;
_versionId = [HippyBundleURLProvider parseVersionId:_debugPathUrl];
}
}

- (NSString *)localhost {
return [NSString stringWithFormat:@"%@:%@", _localhostIP, _localhostPort];
}

- (NSString *)bundleURLString {
NSString *scheme = _scheme.length > 0 ? _scheme : HippyBundleURLSchemeHttp;
return [NSString stringWithFormat:@"%@://%@%@", scheme, self.localhost, self.debugPathUrl];
}

@end
Loading

0 comments on commit 0aea441

Please sign in to comment.