将 C/C++ 套接字传递到 EM_ASM 以在 Emscripten 中用作 websocket
Passing a C/C++ socket into EM_ASM to use as websocket in Emscripten
在我的 C++ 程序中,我想做这样的事情
int mySock = socket(...);
EM_ASM_({
mySock.send("hello");
},mySock);
传递 c++ 套接字以便我可以将其用作 javascript websocket 的正确方法是什么?
编辑:
BSD-style Emscripten 中的套接字 C/C++ 是 websocket,因为 WebAssembly 缺少 lower-level 网络 API 用于浏览器。
所以你的目标是将 C/C++ 套接字传递给 EM_ASM
JS 块。在深入挖掘 Emscripten 的源代码后 I found that WS.sockets[id]
should work:
#include <stdio.h>
#include <emscripten.h>
int mySock = socket(...);
EM_ASM({ // Or MAIN_THREAD_EM_ASM instead.
var ws = WS.sockets[[=10=]];
// Play with your socket here...
}, mySock);
我知道这听起来没有记录在案的骇人听闻的行为,但此时此刻,当您想要在 Emscripten 中进行一些 low-level 互操作时,您应该处理骇客问题。
原创(此处有误,忽略)
Emscripten 中的 BSD 套接字 C/C++ 是网络套接字,因为浏览器缺少 lower-level 网络 API。
所以你的目标是将 C/C++ 套接字传递给 EM_ASM
JS 块。经过一番搜索 I found a hidden (undocumented) API called FS.getStream()
。我还没有测试它,因为我目前没有合适的测试环境,但你可以这样试试:
#include <stdio.h>
#include <emscripten.h>
int mySock = socket(...);
EM_ASM_({ // Or MAIN_THREAD_EM_ASM instead.
var stream = FS.getStream([=11=]);
var data = new Uint8Array(32);
FS.write(stream, data, 0, data.length, 0);
}, mySock);
有关 FS
API 的更多信息(尽管 FS.getStream()
是少数未记录的方法之一),您可以从 the Emscripten official documentation.
中找到它
在我的 C++ 程序中,我想做这样的事情
int mySock = socket(...);
EM_ASM_({
mySock.send("hello");
},mySock);
传递 c++ 套接字以便我可以将其用作 javascript websocket 的正确方法是什么?
编辑:
BSD-style Emscripten 中的套接字 C/C++ 是 websocket,因为 WebAssembly 缺少 lower-level 网络 API 用于浏览器。
所以你的目标是将 C/C++ 套接字传递给 EM_ASM
JS 块。在深入挖掘 Emscripten 的源代码后 I found that WS.sockets[id]
should work:
#include <stdio.h>
#include <emscripten.h>
int mySock = socket(...);
EM_ASM({ // Or MAIN_THREAD_EM_ASM instead.
var ws = WS.sockets[[=10=]];
// Play with your socket here...
}, mySock);
我知道这听起来没有记录在案的骇人听闻的行为,但此时此刻,当您想要在 Emscripten 中进行一些 low-level 互操作时,您应该处理骇客问题。
原创(此处有误,忽略)
Emscripten 中的 BSD 套接字 C/C++ 是网络套接字,因为浏览器缺少 lower-level 网络 API。
所以你的目标是将 C/C++ 套接字传递给 EM_ASM
JS 块。经过一番搜索 I found a hidden (undocumented) API called FS.getStream()
。我还没有测试它,因为我目前没有合适的测试环境,但你可以这样试试:
#include <stdio.h>
#include <emscripten.h>
int mySock = socket(...);
EM_ASM_({ // Or MAIN_THREAD_EM_ASM instead.
var stream = FS.getStream([=11=]);
var data = new Uint8Array(32);
FS.write(stream, data, 0, data.length, 0);
}, mySock);
有关 FS
API 的更多信息(尽管 FS.getStream()
是少数未记录的方法之一),您可以从 the Emscripten official documentation.