国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

使用Node.js為其他_程序編寫擴展的基本方法

2019-11-02 15:48:20
字體:
來源:轉載
供稿:網友

   這篇文章主要介紹了使用Node.js為其他程序編寫擴展的基本方法 ,文中示例是通過Node讓JavaScript代碼與C++應用產生交互,需要的朋友可以參考下

  準備開始

  首先我們用下面的目錄結構來創建一個節點通知(node-notify)文件夾.

   代碼如下:

  .

  |-- build/ # This is where our extension is built.

  |-- demo/

  | `-- demo.js # This is a demo Node.js script to test our extension.

  |-- src/

  | `-- node_gtknotify.cpp # This is the where we do the mapping from C++ to Javascript.

  `-- wscript # This is our build configuration used by node-waf

  這個看起來很漂亮的tree 用通用的 tree 生成.

  現在讓我來創建測試腳本demo.js 和決定我們擴展的API前期看起來應該像:

  ?

  1

  2

  3

  4

  5

  6

  7

  8// This loads our extension on the notify variable.

  // It will only load a constructor function, notify.notification().

  var notify = require("../build/default/gtknotify.node"); // path to our extension

  var notification = new notify.notification();

  notification.title = "Notification title";

  notification.icon = "emblem-default"; // see /usr/share/icons/gnome/16x16

  notification.send("Notification message");

  編寫我們的Node.js擴展

  Init方法

  為了創建一個Node.js擴展,我們需要編寫一個繼承node::ObjectWrap的C++類。 ObjectWrap 實現了讓我們更容易與Javascript交互的公共方法

  我們先來編寫類的基本框架:

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37#include // v8 is the Javascript engine used by QNode

  #include

  // We will need the following libraries for our GTK+ notification

  #include

  #include

  #include

  using namespace v8;

  class Gtknotify : node::ObjectWrap {

  private:

  public:

  Gtknotify() {}

  ~Gtknotify() {}

  static void Init(Handle target) {

  // This is what Node will call when we load the extension through require(), see boilerplate code below.

  }

  };

  /*

  * WARNING: Boilerplate code ahead.

  *

  * See https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/ & http://www.freebsd.org/cgi/man.cgi?query=dlsym

  *

  * Thats it for actual interfacing with v8, finally we need to let Node.js know how to dynamically load our code.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浦东新区| 临桂县| 邓州市| 河津市| 明光市| 余江县| 盈江县| 广德县| 顺平县| 丰台区| 武功县| 离岛区| 夏津县| 兴安盟| 嘉荫县| 鲜城| 祁连县| 宁国市| 黔江区| 濮阳市| 疏勒县| 榆中县| 阿坝县| 紫阳县| 湘西| 嘉善县| 道孚县| 洪湖市| 盖州市| 渝中区| 桂阳县| 阳东县| 孝义市| 青岛市| 泗洪县| 锦州市| 册亨县| 凤台县| 阿巴嘎旗| 乌什县| 化德县|