博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nodejs扩展,实现消息弹窗
阅读量:6993 次
发布时间:2019-06-27

本文共 4094 字,大约阅读时间需要 13 分钟。

參考的实现

  

模块的C++代码 node_gtknotify.cc

#include <v8.h>

#include <node.h>

#include <string>

#include <gtkmm-3.0/gtkmm.h>

#include <libnotifymm.h>

  

using namespace v8;

  

class GtkNotify : node::ObjectWrap{

public:

GtkNotify(){}

~GtkNotify(){}

  

std::string title;

std::string icon;

  

static Persistent<FunctionTemplate> persistent_function_template;

  

static void Init(Handle<Object> target){

HandleScope scope;

Local<FunctionTemplate> local_function_template = FunctionTemplate::New(New);

GtkNotify::persistent_function_template = Persistent<FunctionTemplate>::New(local_function_template);

GtkNotify::persistent_function_template->InstanceTemplate()->SetInternalFieldCount(1);

GtkNotify::persistent_function_template->SetClassName(String::NewSymbol("Notification"));

GtkNotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("title"), GetTitle, SetTitle);

GtkNotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("icon"), GetIcon, SetIcon);

NODE_SET_PROTOTYPE_METHOD(GtkNotify::persistent_function_template, "send", Send);

target->Set(String::NewSymbol("notification"), GtkNotify::persistent_function_template->GetFunction());

}

  

static Handle<Value> New(const Arguments& args){

HandleScope scope;

GtkNotify* instance = new GtkNotify();

instance->title = "Node.js";

instance->icon = "terminal";

instance->Wrap(args.This());

return args.This();

}

  

static Handle<Value> Send(const Arguments& args){

HandleScope scope;

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(args.This());

String::Utf8Value v8str(args[0]);

//弹出消息框

Notify::init("Basic");

Notify::Notification n(instance->title.c_str(), *v8str, instance->icon.c_str());

n.show();

return Boolean::New(true);

}

  

static Handle<Value> GetTitle(Local<String> property, const AccessorInfo& info){

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

return String::New(instance->title.c_str());

}

  

static Handle<Value> GetIcon(Local<String> property, const AccessorInfo& info){

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

return String::New(instance->icon.c_str());

}

  

static void SetTitle(Local<String> property, Local<Value> value, const AccessorInfo& info) {

        GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

        String::Utf8Value v8str(value);

        instance->title = *v8str;

}

  

static void SetIcon(Local<String> property, Local<Value> value, const AccessorInfo& info) {

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

String::Utf8Value v8str(value);

instance->icon = *v8str;

}

};

  

Persistent<FunctionTemplate> GtkNotify::persistent_function_template;

  

extern "C"{

static void init(Handle<Object> target){

GtkNotify::Init(target);

}

NODE_MODULE(node_gtknotify, init);

}

  

node-gyp配置文件 binding.gyp

{

"targets": [

{

"target_name": "node_gtknotify",

"sources": [ "src/node_gtknotify.cc" ]

}

]

}

  

文件夹结构

  

执行命令

node-gyp configure

node-gyp build

  

假设没有安装对应的库/路径找不到。中间会出现头文件找不到的错误;

笨拙的解决方法。在build以下的Makefile中加入

CXXFLAGS += -I/usr/include/glibmm-2.4 -I/usr/lib/x86_64-linux-gnu/glibmm-2.4/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/sigc++-2.0 -I/usr/lib/x86_64-linux-gnu/sigc++-2.0/include -I/usr/include/giomm-2.4 -I/usr/include/gdkmm-3.0 -I/usr/lib/x86_64-linux-gnu/gdkmm-3.0/include -I/usr/include/pangomm-1.4 -I/usr/lib/x86_64-linux-gnu/pangomm-1.4/include -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairomm-1.0 -I/usr/include/freetype2 -I/usr/include/gtkmm-3.0 -I/usr/lib/x86_64-linux-gnu/gtkmm-3.0/include -I/usr/include/atkmm-1.6 -I/usr/include/atk-1.0 -I/usr/include/libnotifymm-1.0

  

上述的库是依据错误提示进行安装的

  

Javascript測试代码

var notify = require("./build/Release/node_gtknotify");

var notification = new notify.notification();

notification.title = "Notification title";

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

notification.send("hello,world");

  

注意,执行时可能有错误提示:**符号找不到,这是由于没有加入对应的共享链接库

  

  

解决方法:在build/node_gtknotify.target.mk中加入

LIBS := -lglibmm-2.4 -lnotify -lnotifymm-1.0

  

执行效果

你可能感兴趣的文章
Kubernetes为何如此炙手可热?
查看>>
Spring Auto-Wiring Beans
查看>>
php不重新编译添加模块 php不重新编译添加模块
查看>>
jQuery过滤选择器:not()方法
查看>>
致那些没有打倒你的人
查看>>
开源监控系统整合Nagios+Cacti+Nconf+Npc中文版
查看>>
SeimiCrawler v0.24发布了
查看>>
腾讯微信公众平台账号类型说明
查看>>
UNIX域socket编程
查看>>
python学习---数据结构(一)
查看>>
MySQL移动数据目录出现权限问题
查看>>
android 试图 代码 关联
查看>>
内嵌cuzySDK的App——礼物购已登陆App store
查看>>
linux-centos6.5 64位 安装nfs 过程
查看>>
MySQL中的字符串模式匹配
查看>>
互联网金融现状与趋势——王斌
查看>>
mac 终端命令-1
查看>>
DETweetComposeViewController
查看>>
JVM参数---常用参数配置
查看>>
python 描述符
查看>>