JSBridge高并发时H5只能收到一个或两个回调,其原因是
- (void)flushMessageQueue:(NSString*)messageQueueString
方法里responseId被覆盖,导致返回H5多个同样responseId的回调
解决方法是修改
- (void)flushMessageQueue:(NSString*)messageQueueString{
if(messageQueueString ==nil|| messageQueueString.length==0) {
NSLog(@"WebViewJavascriptBridge: WARNING: ObjC got nil while fetching the message queue JSON from webview. This can happen if the WebViewJavascriptBridge JS is not currently present in the webview, e.g if the webview just loaded a new page.");
return;
}
idmessages = [self_deserializeMessageJSON:messageQueueString];
for(WVJBMessage* messageinmessages) {
if(![messageisKindOfClass:[WVJBMessageclass]]) {
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [message class], message);
continue;
}
[self_log:@"RCVD"json:message];
NSString* responseId = message[@"responseId"];
if(responseId) {
WVJBResponseCallback responseCallback =_responseCallbacks[responseId];
responseCallback(message[@"responseData"]);
[self.responseCallbacksremoveObjectForKey:responseId];
}else{
WVJBResponseCallback responseCallback =NULL;
NSString* callbackId = message[@"callbackId"];
if(callbackId) {
responseCallback = ^(id responseData) {
if(responseData ==nil) {
responseData = [NSNull null];
}
WVJBMessage* msg;
//此处检测回调数据中是否含有responseId,如果responseData中有responseId则直接返回responseData,如果没有走原逻辑
if([responseData objectForKey:@"responseId"]) {
NSMutableDictionary*responseSend = [[NSMutableDictionary alloc]initWithDictionary:responseData];
[responseSendremoveObjectForKey:@"responseId"];
msg =@{@"responseId":[responseData objectForKey:@"responseId"],@"responseData":responseSend};
}else{
msg =@{@"responseId":callbackId,@"responseData":responseData};
}
[self_queueMessage:msg];
};
}else{
responseCallback = ^(idignoreResponseData) {
// Do nothing
};
}
WVJBHandlerhandler =self.messageHandlers[message[@"handlerName"]];
if(!handler) {
NSLog(@"WVJBNoHandlerException, No handler for message from JS: %@", message);
continue;
}
//此处返回整个message,其中包含responseId,等原生请求完成后把responseId添加进请求返回的数据
handler(message, responseCallback);
}
}
}
注意:原生请求时保存responseId最好以接口名为key保存,高并发请求回调给H5时才不会出错