如何使用javascript库将Socket.io与emscripten一起使用?
我使用EnScript将一个C++项目移植到Web中,和我的C++代码交互的Web应用程序是在NoDEJS上。如何使用javascript库将Socket.io与emscripten一起使用?,javascript,node.js,sockets,socket.io,emscripten,Javascript,Node.js,Sockets,Socket.io,Emscripten,我使用EnScript将一个C++项目移植到Web中,和我的C++代码交互的Web应用程序是在NoDEJS上。 >我在NoDE.js上使用SokKo.IO,我也想用C++代码使用它,所以我使用了SoCKET.IO代码的JavaScript库,但是它似乎不起作用。 我写了一个小例子来演示这个案例: #include <iostream> #include <stdlib.h> #include <stdio.h> #include <emscripten
>我在NoDE.js上使用SokKo.IO,我也想用C++代码使用它,所以我使用了SoCKET.IO代码的JavaScript库,但是它似乎不起作用。 我写了一个小例子来演示这个案例:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <emscripten.h>
int val = 0;
extern "C"
{
extern void send_board(char* flat_board);
extern bool receive_board(char** _string_dest);
}
void one_iter()
{
#ifdef SEND
char* c = "test";
std::cout << val << std::endl;
if(val%1000 == 0){
send_board(c);
}
val++;
#else
char* c;
if(receive_board(&c)) std::cout << "Received:" << c << std::endl;
#endif
}
int main()
{
emscripten_set_main_loop(one_iter, 0, 1);
return 0;
}
我用以下方法编译:
// for the sending test
em++ main.cpp --js-library path_to_js_library.js -o socket.html -DSEND=1
// for the receiving test
em++ main.cpp --js-library path_to_js_library.js -o socket.html
在Node.Js服务器代码中,我有:
io.on('connection', function (socket) {
socket.on('game_request_sender_pipeline' , function (message)
{
console.log("game_request_sender_pipeline on.");
socket.broadcast.emit('game_request_receiver_pipeline', {
message: message.message,
});
console.log("game_request_receiver_pipeline emitted.");
});
});
结果非常奇怪,直到我认为它不起作用,我取消了nodejs服务器并重新启动它,然后结果在浏览器控制台中弹出。评论中的建议似乎对他们有意义
emscripten\u set\u main\u循环
将模拟同步行为,但是由于socket.io的原因,来自javascript api的调用是异步的
因此,为了解决这个问题,我考虑使用回调,而不是使用return
语句并有条件地执行我想要的代码(无论是true还是false)
extern "C"
{
EMSCRIPTEN_KEEPALIVE void callback_invoker(void* fn_ptr, void* fn_arg)
{
// some kind of cast to the original function signature, and to the original fn_arg type
(*fn_ptr)(fn_arg);
}
}
这个想法是这样的:
receive\u board
接收板
将接收成功回调和失败回调作为参数。(回调是C函数)Module.ccall
从c调用回调ccall
,我们必须在函数定义中使用关键字EMSCRIPTEN\u KEEPALIVE
,为了避免为每个将要定义的回调编写该关键字,我决定只对调用回调的一个函数使用它
extern "C"
{
EMSCRIPTEN_KEEPALIVE void callback_invoker(void* fn_ptr, void* fn_arg)
{
// some kind of cast to the original function signature, and to the original fn_arg type
(*fn_ptr)(fn_arg);
}
}
在javascript方面
mergeInto(LibraryManager.library, {
receive_board: function(_string_dest_in_c, succcess_callback, failure_callback, s_cb_arg, f_cb_arg){
socket.on('connect', function(){
socket.on('game_request_receiver_pipeline' , function (message)
{
var msg = "Other side : " + message.message;
var buffer = Module._malloc(message.message.length + 1);
Module.writeStringToMemory(msg, buffer);
setValue(_string_dest_in_c, buffer, '*');
Module.ccal('callback_invoker', 'void', ['number', 'number'], [success_callback, s_cb_arg]);
return;
});
});
Module.ccal('callback_invoker', 'void', ['number', 'number'], [failure_callback, f_cb_arg]);
},
});
这样,我解决了前面提到的问题
受此启发,
one_iter()
是同步的,但是接收板
似乎是异步的。我应该实现回调来解决它的异步性质吗?我想是的。emscripten\u set\u main\u loop
的第三个参数是真的,one_iter被调用了很多次。是的,因为真正的问题发生在一个图形游戏中,我需要最后一个参数为真来模拟一个无限循环,如果我是错误的请纠正我非常聪明的解决方案;没有考虑从JS端动态分配内存!