有没有办法伪造同步XHR请求? [英] Is there a way to fake a synchronous XHR request?

查看:389
本文介绍了有没有办法伪造同步XHR请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Emscripten系统移植一堆C ++ code的为Javascript。 C ++的code有许多呼叫的fopen 这是一个同步IO调用。在Emscripten,我们模拟这种使用XHR请求本地资源的然而的,火狐同步XH​​R调用中(以的responseType 一滴 arraybuffer )仅一个网络工作者中支持。转换所有的C ++ code,以适应异步IO code似乎很复杂,所以我第一次尝试,我想看看我是否可以伪造一个同步XHR请求。

I'm porting a pile of C++ code to Javascript using the Emscripten system. The C++ code has many calls to fopen which is a synchronous IO call. Within Emscripten, we simulate this using an XHR request to a local resource however, within Firefox synchronous XHR calls (with a responseType of blob or arraybuffer) are only supported within a Web-Worker. Converting all that c++ code to adapt to asynchronous IO code seems very complicated so for my first try, I'd like to see if I can fake a synchronous XHR request.

我最初的想法是,主回路可以用一个网络工作者这可能使同步IO调用和更新共享状态,而主循环暂停,等待完成网络工作者分享一些状态。免责声明:我知道这是不是典型的JavaScript的方式,但我端起同步code,而不是从头开始编写新的code(其中我肯定会使用了异步IO)

My initial thought was that the main loop could share some state with a web-worker which could make the synchronous io call and update the shared state while the main loop paused and waited for the web-worker finished. DISCLAIMER: I know this is not the typical Javascript way but I am porting synchronous code, not writing new code from scratch (in which I would definitely have used asynchronous IO).

鉴于网络工作者和主回路之间共享状态的限制,这个想法看起来是站不住脚的。

Given the restrictions on sharing state between a web-worker and the main-loop, this idea looks untenable.

还有没有其他的方法可以做到这一点?

Are there other ways to do this?

推荐答案

所以,看到所有的答案,做一些我自己的阅读后,它出现在最好的答案是:你可以,但仅限于文本数据,你必须将其转换回二进制数据。这是缓慢的,但没有工作。

So after seeing all the answers and doing some of my own reading, it appears the best answer is: "You can, but only for text data and you have to convert it back to binary data". This is slow, but does work.

<一个href=\"https://developer.mozilla.org/en-US/docs/Web/API/XMLHtt$p$pquest/Sending_and_Receiving_Binary_Data?redirectlocale=en-US&redirectslug=DOM%2FXMLHtt$p$pquest%2FSending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers\" rel=\"nofollow\">https://developer.mozilla.org/en-US/docs/Web/API/XMLHtt$p$pquest/Sending_and_Receiving_Binary_Data?redirectlocale=en-US&redirectslug=DOM%2FXMLHtt$p$pquest%2FSending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers

这篇关于有没有办法伪造同步XHR请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆