karosLi / KKJSBridge

一站式解决 WKWebView 支持离线包,Ajax/Fetch 请求,表单请求和 Cookie 同步的问题 (基于 Ajax Hook,Fetch Hook 和 Cookie Hook)
MIT License
693 stars 120 forks source link

如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? #35

Closed DongDongDongDong closed 3 years ago

DongDongDongDong commented 4 years ago

因为好几个请求只有body不同,但是js发送到native侧之后,body都被清空了。

karosLi commented 4 years ago

多个请求会有多个requestId,body 在请求发送完会根据 requestId去做清除,所以按理来说每个请求只会清除自己的body,不会清除别人的body的。

如果你遇到被清空的问题,能提供下你的demo或者调试链接吗?

DongDongDongDong commented 4 years ago

native侧是怎么拿到requestid的呢?

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年8月6日 10:18 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

多个请求会有多个requestId,body 在请求发送完会根据 requestId去做清除,所以按理来说每个请求只会清除自己的body,不会清除别人的body的。

如果你遇到被清空的问题,能提供下你的demo或者调试链接吗?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

karosLi commented 4 years ago

js 会在请求 url 上追加 requestId,然后 native 在 urlprotocol 里解析 url,取出 requestId。

DongDongDongDong commented 4 years ago

有点没太明白,如何在NSURLProtocol中获取js请求的requestid,可否贴下关键代码? 感谢🙏

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年8月7日 16:50 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

js 会在请求 url 上追加 requestId,然后 native 在 urlprotocol 里解析 url,取出 requestId。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

karosLi commented 4 years ago

在这里处理的

KKJSBridgeAjaxURLProtocol

- (void)startLoading {
    NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
    //给我们处理过的请求设置一个标识符, 防止无限循环,
    [NSURLProtocol setProperty:@YES forKey:kKKJSBridgeNSURLProtocolKey inRequest:mutableReqeust];

    NSString *requestId;
    //?KKJSBridge-RequestId=159274166292276828
    if ([mutableReqeust.URL.absoluteString containsString:kKKJSBridgeRequestId]) {
        requestId = [self fetchRequestId:mutableReqeust.URL.absoluteString];
        // 移除临时的请求id键值对
        NSString *reqeustPair = [self fetchRequestIdPair:mutableReqeust.URL.absoluteString];
        if (reqeustPair) {
            NSString *absString = [mutableReqeust.URL.absoluteString stringByReplacingOccurrencesOfString:reqeustPair withString:@""];
            mutableReqeust.URL = [NSURL URLWithString:absString];
        }
    }
    ....
}
DongDongDongDong commented 4 years ago

明白了。我之前一直尝试requestid放进header。 但是fetch-hook完不支持。

DongDongDongDong commented 4 years ago

还有一个问题,想请教一下:native在app运行的某一刻,更新了NSHTTPCookie(eg:set cookie=T301)。 然后此时wkwebview发起ajax请求,会发现并没有带上cookie=T301。 这就是因为NSHTTPCookie没有及时和WK同步导致的,这种情况怎么处理比较好呢? 铁子

karosLi commented 4 years ago

有ajax请求代理了,那在发送请求的时候,更新下 http header - cookie

DongDongDongDong commented 4 years ago

最新cookie在NSHTTPCookie中。 但是请求是前端用js发起的。 这时候在哪里更新HTTP Header cookie 呢?

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年8月17日 10:06 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

有ajax请求代理了,那在发送请求的时候,更新下 http header - cookie

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

karosLi commented 4 years ago

KKJSBridge 会把 hook 的 ajax 请求,交由原生网络库去发送的能力,你如果是自己的代码,也可以这么去做。

image

DongDongDongDong commented 3 years ago

js 会在请求 url 上追加 requestId,然后 native 在 urlprotocol 里解析 url,取出 requestId。

前端发起的请求要手动在url上追加requestID,这样不会对前端项目有侵入吗

karosLi commented 3 years ago

对前端是无感知的啊。这个是框架层面已经做好了

DongDongDongDong commented 3 years ago

框架层追加的requestid是怎么生成来表示当前请求的唯一性的呢

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年11月14日 12:03 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

对前端是无感知的啊。这个是框架层面已经做好了

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

karosLi commented 3 years ago

根据时间戳生成唯一id

DongDongDongDong commented 3 years ago

上线后会发现有时间戳重复的情况

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年11月15日 11:51 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

karosLi commented 3 years ago

上线后会发现有时间戳重复的情况 发自我的iPhone ------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年11月15日 11:51 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

你用的是 KKJSBridge 上线吗?哪个版本,有可以复现的链接吗?

DongDongDongDong commented 3 years ago

很难复现,猜测是因为并发请求,时间戳重复了

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年11月16日 15:27 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

DongDongDongDong commented 3 years ago

方便加个微信吗,我的微信AI641462576

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年11月16日 15:27 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

上线后会发现有时间戳重复的情况 发自我的iPhone … ------------------ 原始邮件 ------------------ 发件人: Karosli <notifications@github.com> 发送时间: 2020年11月15日 11:51 收件人: karosLi/KKJSBridge <KKJSBridge@noreply.github.com> 抄送: KAKA <641462576@qq.com>, Author <author@noreply.github.com> 主题: 回复:[karosLi/KKJSBridge] 如果AJAX短时间内并发同一个url的Post请求,但是却带不同的body,这时候,在Native侧是怎么拿body的? (#35)

你用的是 KKJSBridge 上线吗?哪个版本,有可以复现的链接吗?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

karosLi commented 3 years ago

加了

karosLi commented 3 years ago

和楼主确认,他们是参考的 KKJSBrdge 来做的