据我所知,有两个选项可以将C程序移植到本机客户端:
PPP_InitializeModule和PPP_GetInterface。PPAPI_SIMPLE_REGISTER_MAIN即可。因此,问题是如何在第二种情况下实现JS消息处理(在本机代码中处理JS代码发出的消息)?
发布于 2013-12-05 17:34:22
看看SDK中的示例/演示目录中的一些示例: earth、voronoi、flock、pi_generator和life都使用ppapi_simple。
以下是它的基本工作原理:
当使用ppapi_simple时,所有事件(例如输入事件、来自JavaScript的消息)都会添加到事件队列中。以下代码来自寿命实例 (尽管其中一些代码是修改和未经测试的):
PSEventSetFilter(PSE_ALL);
while (true) {
PSEvent* ps_event;
/* Process all waiting events without blocking */
while ((ps_event = PSEventTryAcquire()) != NULL) {
earth.HandleEvent(ps_event);
PSEventRelease(ps_event);
}
...
}然后HandleEvent确定它是什么类型的事件,并以特定于应用程序的方式处理它:
void ProcessEvent(PSEvent* ps_event) {
...
if (ps_event->type == PSE_INSTANCE_HANDLEINPUT) {
...
} else if (ps_event->type == PSE_INSTANCE_HANDLEMESSAGE) {
// ps_event->as_var is a PP_Var with the value sent from JavaScript.
// See docs for it here: https://developers.google.com/native-client/dev/pepperc/struct_p_p___var
if (ps_event->as_var->type == PP_VARTYPE_STRING) {
const char* message;
uint32_t len;
message = PSInterfaceVar()->VarToUtf8(ps_event->as_var, &len);
// Do something with the message. Note that it is NOT null-terminated.
}
}若要将消息发送回JavaScript,请在消息传递接口上使用PostMessage函数:
PP_Var message;
message = PSInterfaceVar()->VarFromUtf8("Hello, World!", 13);
// Send a string message to JavaScript
PSInterfaceMessaging()->PostMessage(PSGetInstanceId(), message);
// Release the string resource
PSInterfaceVar()->Release(message);您也可以发送和接收其他JavaScript类型: ints、浮点数、数组、数组缓冲区和字典。还请参阅VarArray、VarArrayBuffer和VarDictionary接口。
https://stackoverflow.com/questions/20391940
复制相似问题