在 Emscripten C++ / wbasm 中如何获得 "on page closed" 事件
In Emscripten C++ / wbasm how does one get an "on page closed" event
我有一个使用 emscripten 系统编译成 web assebmbly 的 C+ 程序。当他页面 运行 程序关闭时,我想清理一些东西,刷新文件等等。
主要有:
emscripten_set_main_loop_arg(onMainLoopTick, arg, 0, 1);
当前,当页面关闭时,"process" 只是退出,不会在 "loop simulator" 之后继续。我想我需要从页面获取一个事件,该事件将阻塞主线程,直到 C++ 代码处理它并清理它的混乱。
我应该将什么事件转发给 C++,我应该如何使用它?
首先要知道的是,WebAssembly 没有本地库或 API(我的意思是……还没有,as of MVP. There are native features like threads coming as post-MVP feature)。 What is means C++中的所有系统库都是通过导入模拟的JavaScript函数来实现的。因此,如果您正在寻找检测关闭事件等本机功能,您应该检查是否有 JS/HTML5 API 可以执行类似的操作。
要查看它是如何工作的,打开生成的 .wast
文件并直接搜索 import
instructions and generated JS files. Also, you may want to search on Emscripten repo 以检查 C++ 端是否有 JS/HTML5 绑定可用,因为它们的文档非常大很难看透。
直截了当地说,关闭时触发的 HTML5 事件是 beforeunload
and unload
. I would prefer using beforeunload
event. Emscripten provides em_beforeunload_callback
callback function type and emscripten_set_beforeunload_callback
to register in html5.h bindings.
否则,你直接使用它们。例如:
在 C++ 中:
void EMSCRIPTEN_KEEPALIVE clean_stuff() {
// Clean up the mess...
// You should use EMSCRIPTEN_KEEPALIVE or
// add it to EXPORTED_FUNCTIONS in emcc compilation options
// to make it callable in JS side.
}
在 JS 中:
window.addEventListener("beforeunload", function (event) {
// Exported functions are prefixed by an underscore
Module._clean_stuff();
});
我有一个使用 emscripten 系统编译成 web assebmbly 的 C+ 程序。当他页面 运行 程序关闭时,我想清理一些东西,刷新文件等等。
主要有:
emscripten_set_main_loop_arg(onMainLoopTick, arg, 0, 1);
当前,当页面关闭时,"process" 只是退出,不会在 "loop simulator" 之后继续。我想我需要从页面获取一个事件,该事件将阻塞主线程,直到 C++ 代码处理它并清理它的混乱。
我应该将什么事件转发给 C++,我应该如何使用它?
首先要知道的是,WebAssembly 没有本地库或 API(我的意思是……还没有,as of MVP. There are native features like threads coming as post-MVP feature)。 What is means C++中的所有系统库都是通过导入模拟的JavaScript函数来实现的。因此,如果您正在寻找检测关闭事件等本机功能,您应该检查是否有 JS/HTML5 API 可以执行类似的操作。
要查看它是如何工作的,打开生成的 .wast
文件并直接搜索 import
instructions and generated JS files. Also, you may want to search on Emscripten repo 以检查 C++ 端是否有 JS/HTML5 绑定可用,因为它们的文档非常大很难看透。
直截了当地说,关闭时触发的 HTML5 事件是 beforeunload
and unload
. I would prefer using beforeunload
event. Emscripten provides em_beforeunload_callback
callback function type and emscripten_set_beforeunload_callback
to register in html5.h bindings.
否则,你直接使用它们。例如:
在 C++ 中:
void EMSCRIPTEN_KEEPALIVE clean_stuff() {
// Clean up the mess...
// You should use EMSCRIPTEN_KEEPALIVE or
// add it to EXPORTED_FUNCTIONS in emcc compilation options
// to make it callable in JS side.
}
在 JS 中:
window.addEventListener("beforeunload", function (event) {
// Exported functions are prefixed by an underscore
Module._clean_stuff();
});