当前位置: 代码迷 >> Web前端 >> chrome native client牛刀小试
  详细解决方案

chrome native client牛刀小试

热度:845   发布时间:2012-09-05 15:19:34.0
chrome native client小试牛刀

一、环境准备

1、安装native client sdk

????下载地址: http://commondatastorage.googleapis.com/nativeclient-mirror/nacl/nacl_sdk/naclsdk_win.exe

下载后安装在C:\native_client_sdk_0_5_1052

本次试验的两个关键目录project_templates,examples.其中project_templates包含一个模板代码生成,examples中包含示例代码。

2、安装python

下载地址:http://www.python.org/ftp/python/2.7.2/python-2.7.2.msi
下载后安装到C:\Python27,在系统环境变量中的Path最后加上;C:\Python27

3、在chrome中启用native client

在chrome浏览器中地址栏输入about:flags 找到native client点击启动,重启浏览器;然后在浏览器地址栏输入about:plugins找到native client点击启动,无须重启浏览器

二、查看示例

进入C:\native_client_sdk_0_5_1052\examples,双击httpd.py,启动python的web server。

在chrome地址栏输入http://localhost:5103/index.html,可查看自带示例。

三、开发示例

1、创建工程

进入控制台:

>cd C:\native_client_sdk_0_5_1052\project_templates

>?init_project.py -n helloworld -d ../examples (-n 名称 -d目录)

这时在examples文件下多了一个helloworld目录。改目录有:

helloworld.cc c++代码

helloworld.html 网页代码

scons.bat 编译C++

2、修改代码

打开helloworld.html,新增

function moduleDidLoad() {
      HelloTutorialModule = document.getElementById('hello_tutorial');
      HelloTutorialModule.addEventListener('message', handleMessage, false);
      updateStatus('SUCCESS');
      //Send a message to the NaCl module.
      HelloTutorialModule.postMessage('hello');
    }

?

?

?

?

?

HelloTutorialModule.postMessage('hello');将hello字符串提交给c++端处理

?

打开helloworld.cc

在#include之后添加

namespace {
// The expected string sent by the browser.
const char* const kHelloString = "hello";
// The string sent back to the browser upon receipt of a message
// containing "hello".
const char* const kReplyString = "hello from NaCl";
} // namespace

?

?

找到

// TODO(sdk_user): 1. Make this function handle the incoming message.

?

?

替换成

?

 if (!var_message.is_string())
    return;
  std::string message = var_message.AsString();
  pp::Var var_reply;
  if (message == kHelloString) {
    var_reply = pp::Var(kReplyString);
    PostMessage(var_reply);
  }

?

?

?


编译C++的native client module ,双击helloworld目录底下的scons.bat文件。

?

?

编译完成后,生成:

?

  • hello_tutorial_x86_32.nexe
  • hello_tutorial_x86_32_dbg.nexe
  • hello_tutorial_x86_64.nexe
  • hello_tutorial_x86_64_dbg.nexe

进入C:\native_client_sdk_0_5_1052\examples

双击httpd.py

在chrome浏览器中输入地址:http://localhost:5103/helloworld/helloworld.html

得到从native client module返回的hello from NaCl

?

1 楼 cheng830306 2011-12-16  
谢。。。。正在找这方面的资料。。。
  相关解决方案