首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ppapi_simple进行消息处理

用ppapi_simple进行消息处理
EN

Stack Overflow用户
提问于 2013-12-05 05:10:36
回答 1查看 951关注 0票数 2

据我所知,有两个选项可以将C程序移植到本机客户端:

  1. 实现许多初始化函数,如PPP_InitializeModulePPP_GetInterface
  2. 只需将主函数传递给PPAPI_SIMPLE_REGISTER_MAIN即可。

因此,问题是如何在第二种情况下实现JS消息处理(在本机代码中处理JS代码发出的消息)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-05 17:34:22

看看SDK中的示例/演示目录中的一些示例: earth、voronoi、flock、pi_generator和life都使用ppapi_simple。

以下是它的基本工作原理:

当使用ppapi_simple时,所有事件(例如输入事件、来自JavaScript的消息)都会添加到事件队列中。以下代码来自寿命实例 (尽管其中一些代码是修改和未经测试的):

代码语言: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确定它是什么类型的事件,并以特定于应用程序的方式处理它:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
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、浮点数、数组、数组缓冲区和字典。还请参阅VarArrayVarArrayBufferVarDictionary接口。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20391940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档