1

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. This is for the new 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?

So if the client passes a 1 the C++ code does one thing and if a 2 it does another. I just can't see how it's doing it.

The JavaScript that seems to start the C++ code running is:

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.

Dan
  • 2,304
  • 6
  • 42
  • 69

1 Answers1

3

All asynchronous messages from JavaScript are handled in the HandleMessage function, defined in your pp::Instance:

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:

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.

binji
  • 1,860
  • 9
  • 9