简体   繁体   English

如何在PPAPI中将变量从JavaScript传递到C ++?

[英]How do you pass a variable from JavaScript to C++ in PPAPI?

I'm looking at someones code (not contactable) and they have set up a websocket client that talks to a server and runs some C++ code. 我正在查看某人的代码(不可联系),他们已经设置了一个与服务器对话并运行一些C ++代码的websocket客户端。 This is for the new Chrome PPAPI. 这是用于新的Chrome PPAPI。 I can pass variables from the client to the server but I'm not sure how to relay them to the C++ side of things to alter what happens in the code? 我可以将变量从客户端传递到服务器,但是我不确定如何将其中继到C ++端以更改代码中发生的事情?

So if the client passes a 1 the C++ code does one thing and if a 2 it does another. 因此,如果客户端传递1,则C ++代码执行一件事,如果传递2,则另一件事。 I just can't see how it's doing it. 我只是看不到它是怎么做的。

The JavaScript that seems to start the C++ code running is: 似乎可以启动C ++代码运行的JavaScript是:

function onStartTest(e)
{

    console.log('Start Test');
    var hostname = document.getElementById('hostname').value;
    var message = 'start:'+hostname;
    common.logMessage(message);
    common.naclModule.postMessage(message);
    e.preventDefault();
    return false;
}

I've looked at a few examples to no avail. 我看了几个例子都没有用。

All asynchronous messages from JavaScript are handled in the HandleMessage function, defined in your pp::Instance: 来自JavaScript的所有异步消息都在pp :: Instance中定义的HandleMessage函数中处理:

class MyInstance : public pp::Instance {
  ...
  virtual void HandleMessage(const pp::Var& message) {
    ...
  }
};

In your example, you are sending a string value. 在您的示例中,您正在发送一个字符串值。 To extract it, you can use the pp::Var::AsString method: 要提取它,可以使用pp :: Var :: AsString方法:

virtual void HandleMessage(const pp::Var& message) {
  if (message.is_string()) {
    std::string str_message = message.AsString();
    ...
  }
}

If you just want to pass numbers, you can do that too: 如果您只想传递数字,也可以这样做:

virtual void HandleMessage(const pp::Var& message) {
  if (message.is_int()) {
    int32_t int_message = message.AsInt();
    switch (int_message) {
      case 1: ...
      case 2: ...
    }
  }
}

Take a look at the documentation here for more info. 请查看此处的文档以获取更多信息。 You can send ArrayBuffers, and even arbitrary objects as well. 您可以发送ArrayBuffers,甚至可以发送任意对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM