Nodejs扩展,实现消息弹窗
參考https://github.com/olalonde/node-notify的实现
模块的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
执行效果
Nodejs扩展,实现消息弹窗的更多相关文章
- Sagit.Framework For IOS 开发框架入门教程5:消息弹窗STMsgBox
前言: 昨天刚写了一篇IT连创业的文章:IT连创业系列:产品设计之答题模块,(欢迎大伙关注!) 感觉好久没写IOS的文章了,今天趁机,来补一篇,Sagit的教程. Sagit 开源地址:https:/ ...
- 【WPF】对话框/消息弹窗
非模式对话框 需求:弹窗是非模式对话框,即可以多个弹窗弹出,且弹窗后面的窗体可以被操作,不会被锁定. 自定义的窗体Window实现以下步骤: 在C#代码中弹出窗体时,使用 window.Show() ...
- 搭建前端监控系统(五)Nodejs怎么搭建消息队列
怎样定位前端线上问题,一直以来,都是很头疼的问题,因为它发生于用户的一系列操作之后.错误的原因可能源于机型,网络环境,接口请求,复杂的操作行为等等,在我们想要去解决的时候很难复现出来,自然也就无法解决 ...
- UWP笔记-消息弹窗自动淡出
为了让用户有个更好的UI交互,可以增加自动淡出的消息弹窗,例如:网易云音乐UWP,切换播放模式时,出现的类似消息提示. 右键项目,添加用户控件 UserControlDemo.xaml: <Us ...
- 扩展:Flash消息
扩展:Flash消息 flash存值之后只能取一次 from flask import Flask,render_template,flash,get_flashed_messages,session ...
- BOOST 线程完全攻略 - 扩展 - 线程消息通讯
// controlled_module_ex.hpp : controlled_module类的扩展 // 增强线程之间消息通讯 // 增加线程安全启动和安全关闭功能 // 增加定时器功能 #p ...
- js封装好的模仿qq消息弹窗代码
在我们的日常开发中,或者生活中.常常须要用到弹出窗.这里我们就用js模拟一下qq消息一样的弹出窗. 直接贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- Nodejs 接收RabbitMQ消息
参考官方地址:https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html 关于C#消息发送端,请参考<c# RabbitMQ ...
- nodejs EventEmitter 发送消息
var util = require('util'); var evem = require('events').EventEmitter; function myem(){ evem.call(th ...
随机推荐
- java 复习整理(四 String类详解)
String 类详解 StringBuilder与StringBuffer的功能基本相同,不同之处在于StringBuilder是非线程安全的,而StringBuffer是线程安全的,因此效率上S ...
- css垂直居中的几种方式
1. 对于可以一行处理的 设置 height:apx; line-height:apx; 2.对于一段文字(会多行显示的) ->2.1如果是可以设置一个固定高度的 ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 转: wireshark过滤规则
转: http://blog.sina.com.cn/s/blog_48a0f2740100ka71.html WireShark过滤语法 1.过 滤IP,如来源IP或者目标IP等于某个IP 例子: ...
- UVALIVE 3645 Objective: Berlin
最大流 .以航班为节点进行最大流. 容量限制进行拆点. 如果时间地点满足可以建一条边. 具体看代码.变量名被修改过了.一开始的变量名可能比较容易看懂 但CE了.可能与库里的变量重复了. AC代码 #i ...
- source insight setting
adjust source code font size Options -> File Type Options -> Screen Font -> Size adjust dis ...
- resin启动问题
启动resin时报错如下: Resin/4.0.28 can't restart -server 'app-0'. com.caucho.bam.RemoteConnectionFailedExcep ...
- msvc交叉编译:使用vcvarsall.bat设置命令行编译环境
一直以来我只知道vc设置命令行编译环境的批处理命令是%VS140COMNTOOLS%/Common7/Tools下的vsvars32.bat,(%VS140COMNTOOLS%为定义vs2015公共工 ...
- mysql故障(程序正在运行却找不到PID文件)
[root@slave ~]# /etc/init.d/mysql status ERROR! MySQL is running but PID file could not be found [ro ...
- 解决ASP.NET裁剪图片失真
//有的时候剪切图片时,出现图片失真的问题,是因为图片质量下降造成的 //按照指定的数据画出画板(位图) System.Drawing.Image imgPhoto = System.Drawing. ...