Emscripten c++ emscripten_fetch(和 javascript 版本)

问题描述 投票:0回答:1

我',尝试执行 emscripten_fetch() 并总是得到 0 的返回值。不确定发生了什么,我切换到 JavaScript 版本,它可以工作,但有它自己的问题。

C++ - 谁能看到我在这里做错了什么?对我来说,标题数据似乎没有正确设置,因为我收到此错误:

Uncaught TypeError: XMLHttpRequest.open: Cannot convert argument 1 to ByteString because the character at index 2 has value 56770 which is greater than 255.

这是代码:

  emscripten_fetch_attr_t attr;
  emscripten_fetch_attr_init(&attr);

  strcpy(attr.requestMethod, "POST");
  attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_WAITABLE;

  const char *headers[] = {"Content-Type", "application/json", 0};
  attr.requestHeaders = headers;

  attr.requestData = request.c_str() ;
  attr.requestDataSize = request.length() ;
    
qDebug("making fetch with URL: %s  data: %s", req.c_str(), attr.requestData) ;
  emscripten_fetch_t *fetch = emscripten_fetch(&attr, "http://172.23.4.90/jsonrpc"); // Blocks here until the operation is complete.

  EMSCRIPTEN_RESULT ret = EMSCRIPTEN_RESULT_TIMED_OUT;
  while(ret == EMSCRIPTEN_RESULT_TIMED_OUT)
  {
     // possibly do some other work;
      ret = emscripten_fetch_wait(fetch, 5000) ; // milliseconds to wait, 0 to just poll, INFINITY=wait until completion
  }

  if (fetch->status == 200) {
qDebug("Finished downloading %llu bytes from URL %s.", fetch->numBytes, fetch->url);
    // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];

    retval.append(fetch->data, fetch->numBytes) ;

  } else {
qDebug("Downloading %s failed, HTTP failure status code: %d.", fetch->url, fetch->status);
//        throw jsonrpccxx::JsonRpcException(-32003, "client connector error, received status != 200");
  }
  emscripten_fetch_close(fetch);

我已经尝试过等待,并完全同步相同的问题。

结果,我尝试用 javascript 编写它,神奇的是,它成功了! console.log() 输出了我期望的结果。但是,我的 C++ 程序返回的只是一个对象承诺。我不是 Javascript 人,不知道发生了什么。

所以,我的问题:

  1. emscripten_fetch() 是否可以处理 POST 标头数据?
  2. 有人可以向我展示一个可以运行的 javascript (EM_JS) 版本吗?

杰拉德

javascript promise emscripten
1个回答
0
投票

这有效:

attr.requestData = "abcdef";

但奇怪的是,这不是:

attr.requestData = request.c_str();
© www.soinside.com 2019 - 2024. All rights reserved.