jsbridge与通信模型

Posted feng9exe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsbridge与通信模型相关的知识,希望对你有一定的参考价值。

三层通信模型:

应用层、解释层、会话层;

 

通信协议:

通信原语;

报文格式;

 

网络层:

_evaluatejavascript

会话层:

#define kQueueHasMessage   @"__wvjb_queue_message__"

#define kBridgeLoaded      @"__bridge_loaded__"

表示层:

NSString* responseId = message[@"responseId"];

WVJBResponseCallback responseCallback = _responseCallbacks[responseId];

应用层:

[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response)

        NSLog(@"testJavascriptHandler responded: %@", response);

    ];

 

 

通信机制:

1、js发送消息

sendMessageQueue.push(message);

messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + ‘://‘ + QUEUE_HAS_MESSAGE;

2、oc端接收消息

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]];

[_base flushMessageQueue:messageQueueString];

3、oc端消息处理:

WVJBHandler handler = self.messageHandlers[message[@"handlerName"]];

handler(message[@"data"], responseCallback);

构建返回报文并返回:

NSString* callbackId = message[@"callbackId"];

            if (callbackId)

                responseCallback = ^(id responseData)

                    if (responseData == nil)

                        responseData = [NSNull null];

                   

                    

                    WVJBMessage* msg = @ @"responseId":callbackId, @"responseData":responseData ;

                    [self _queueMessage:msg];

                ;

           

 

4、oc端发送消息

NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC(‘%@‘);", messageJSON];

[self _evaluateJavascript:javascriptCommand];

 

 

通信原语:通信控制

#define kOldProtocolScheme @"wvjbscheme"

#define kNewProtocolScheme @"https"

#define kQueueHasMessage   @"__wvjb_queue_message__"

#define kBridgeLoaded      @"__bridge_loaded__"

 

https://__bridge_loaded__/

https://__wvjb_queue_message__/

 

通信报文:

    callbackId = "cb_1_1560136340114";

    data =    

        foo = bar;

    ;

    handlerName = testObjcCallback;

 

会话维护:

callbackId

responseId

 

应用协议:

handlerName

 

生命周期管理

https://__bridge_loaded__/

 

以上是关于jsbridge与通信模型的主要内容,如果未能解决你的问题,请参考以下文章

利用Proxy,如何优雅实现JSBridge模块化封装

Android之利用JSBridge库实现Html,JavaScript与Android的所有交互

Android中实现Native与H5的通信方案汇总

Android中实现Native与H5的通信方案汇总

C/S通信模型与B/S通信模型介绍

JSBridge:混合开发中的双向通信[AndroidiOS&JavaScript]