Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): websocket url add hash and close with code and reason #1447

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
5 changes: 5 additions & 0 deletions examples/hippy-vue-demo/src/components/demos/index.js
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;
6 changes: 6 additions & 0 deletions examples/ios-demo/HippyDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
787C19A22726A1020062EBFF /* HippyInspectorDomain.m in Sources */ = {isa = PBXBuildFile; fileRef = 787C199A2726A1020062EBFF /* HippyInspectorDomain.m */; };
787C19A32726A1020062EBFF /* HippyInspector.m in Sources */ = {isa = PBXBuildFile; fileRef = 787C199B2726A1020062EBFF /* HippyInspector.m */; };
7882DFDC272929D300C12149 /* HippyCSSPropsDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 7882DFDB272929D300C12149 /* HippyCSSPropsDefine.m */; };
789AD030278EDC3200A18C62 /* HippyDevInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 789AD02F278EDC3200A18C62 /* HippyDevInfo.m */; };
851AD1F626564568007AF2B0 /* unicode_string_view.cc in Sources */ = {isa = PBXBuildFile; fileRef = 851AD1F526564568007AF2B0 /* unicode_string_view.cc */; };
854FC5DC262F0D88002EAD41 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = 854FC5CC262F0D88002EAD41 /* .gitignore */; };
854FC5DD262F0D88002EAD41 /* .clang-format in Resources */ = {isa = PBXBuildFile; fileRef = 854FC5CD262F0D88002EAD41 /* .clang-format */; };
Expand Down Expand Up @@ -521,6 +522,8 @@
787C199B2726A1020062EBFF /* HippyInspector.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HippyInspector.m; sourceTree = "<group>"; };
787C19A6272801BF0062EBFF /* HippyCSSPropsDefine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HippyCSSPropsDefine.h; sourceTree = "<group>"; };
7882DFDB272929D300C12149 /* HippyCSSPropsDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HippyCSSPropsDefine.m; sourceTree = "<group>"; };
789AD02E278EDC3200A18C62 /* HippyDevInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HippyDevInfo.h; sourceTree = "<group>"; };
789AD02F278EDC3200A18C62 /* HippyDevInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HippyDevInfo.m; sourceTree = "<group>"; };
851AD1F32656388E007AF2B0 /* string_view_utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = string_view_utils.h; sourceTree = "<group>"; };
851AD1F526564568007AF2B0 /* unicode_string_view.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = unicode_string_view.cc; sourceTree = "<group>"; };
854FC5C8262F0D88002EAD41 /* logging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = logging.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1638,6 +1641,8 @@
F4E2AD78271FC23500E36EFB /* HippyDevWebSocketClient.m */,
F417B54B2727ACCE00894090 /* HippyDevManager.h */,
F417B54C2727ACCE00894090 /* HippyDevManager.m */,
789AD02E278EDC3200A18C62 /* HippyDevInfo.h */,
789AD02F278EDC3200A18C62 /* HippyDevInfo.m */,
);
path = devtools;
sourceTree = "<group>";
Expand Down Expand Up @@ -1881,6 +1886,7 @@
787C19A22726A1020062EBFF /* HippyInspectorDomain.m in Sources */,
064C59FB23AB1A51001E80DD /* HippyEventObserverModule.m in Sources */,
787C19A32726A1020062EBFF /* HippyInspector.m in Sources */,
789AD030278EDC3200A18C62 /* HippyDevInfo.m in Sources */,
064C5A1723AB1A51001E80DD /* HippyTextField.m in Sources */,
064C5A2623AB1A51001E80DD /* HippyViewManager.mm in Sources */,
064C5A1F23AB1A51001E80DD /* HippyImageViewManager.m in Sources */,
Expand Down
39 changes: 39 additions & 0 deletions 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 examples/ios-demo/HippyDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ - (void)viewDidLoad {

#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 @@ -481,19 +481,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 @@ -763,7 +766,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 @@ -250,6 +250,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 All @@ -216,6 +217,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 @@ -310,6 +313,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