是否可以通过WebAssembly提交HTTP请求? [英] Is it possible to submit HTTP requests with WebAssembly?

查看:260
本文介绍了是否可以通过WebAssembly提交HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WebAssembly中提交一个简单的HTTP GET请求.为此,我编写了此程序(从脚本站点复制稍作修改):

I'm trying to submit a simple HTTP GET request in WebAssembly. For this purpose, I wrote this program (copied from Emscripten site with slight modifications):

#include <stdio.h>
#include <string.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/fetch.h>
#include <emscripten.h>
#endif


void downloadSucceeded(emscripten_fetch_t *fetch) {
  printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
  // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
  emscripten_fetch_close(fetch); // Free data associated with the fetch.
}

void downloadFailed(emscripten_fetch_t *fetch) {
  printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
  emscripten_fetch_close(fetch); // Also free data on failure.
}

unsigned int EMSCRIPTEN_KEEPALIVE GetRequest() {
  emscripten_fetch_attr_t attr;
  emscripten_fetch_attr_init(&attr);
  strcpy(attr.requestMethod, "GET");
  attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
  attr.onsuccess = downloadSucceeded;
  attr.onerror = downloadFailed;
  emscripten_fetch(&attr, "http://google.com");
  return 1;
}

当我使用 $ EMSCRIPTEN/emcc main.c -O1 -s MODULARIZE = 1 -s WASM = 1 -o main.js --emrun -s FETCH = 1 进行编译时,我得到了错误

When I compile it using $EMSCRIPTEN/emcc main.c -O1 -s MODULARIZE=1 -s WASM=1 -o main.js --emrun -s FETCH=1 I get the error

ERROR:root:FETCH not yet compatible with wasm (shared.make_fetch_worker is asm.js-specific)

是否可以从WebAssembly运行HTTP请求?如果是,该怎么办?

Is there a way to run HTTP requests from WebAssembly? If yes, how can I do it?

更新1:以下代码尝试发送 GET 请求,但由于CORS问题而失败.

Update 1: The following code attempts to send a GET request, but fails due to CORS issues.

#include <stdio.h>
#include <string.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/fetch.h>
#include <emscripten.h>
#endif

unsigned int EMSCRIPTEN_KEEPALIVE GetRequest() {
  EM_ASM({
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://google.com");
    xhr.send();
  });
  return 1;
}

推荐答案

我最近在esmcripten中发现了此问题,并将其修复为: https://github.com/kripken/emscripten/pull/7010

I recently ran across this issue with esmcripten fixed it in: https://github.com/kripken/emscripten/pull/7010

您现在应该可以同时使用FETCH = 1和WASM = 1.

You should now be able to use FETCH=1 and WASM=1 together.

这篇关于是否可以通过WebAssembly提交HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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