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 ...
随机推荐
- 《R语言实战》读书笔记--为什么要学
本人最近在某咨询公司实习,涉及到了一些数据分析的工作,用的是R语言来处理数据.但是在应用的过程中,发现用R很不熟练,所以再打算学一遍R.曾经花一个月的时间看过一遍<R语言编程艺术>,还用R ...
- jquery使用--常见前端效果实现
一.选中左右侧内容到另一侧:选中左侧内容到右侧,选中右侧内容到左侧 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/ ...
- Hibernate中用注解配置一对多双向关联和多对一单向关联
Hibernate中用注解配置一对多双向关联和多对一单向关联 Hibernate提供了Hibernate Annotations扩展包,使用注解完成映射.在Hibernate3.3之前,需单独下载注解 ...
- http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/
转载自:http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/ 推荐他的所有博文~ 图片 ...
- 10个简化Web开发者工作的HTML5开发工具
HTML5的到来,改变了设计和开发的工作,完全改变了以前的开发方式. HTML5进行本身就是一个很简单,很快捷的开发技术并且带给开发人员很多不同的工具和功能,使他们的工作变得更加Cool.它的功能非常 ...
- CentOS下SVN使用
1. 介绍 这里想在CentOS上搭建的是基于http访问的SVN Server 2. 软件 安装相关软件 yum install httpd httpd-devel mod_dav_svn subv ...
- android的百度地图开发(一)
1,注册百度开发者账号 2,申请key ,注意开发版SH和发布版的SH 获取开发版SHA1: 输入命令:keytool -list -v -keystore debug.keystore,回车输入 ...
- failed to push some refs to 'git@github.com:laniu/liuna.git'报错原因
出现错误的主要原因是github中的README.md文件不在本地代码目录中 可以通过如下命令进行代码合并[注:pull=fetch+merge] git pull --rebase origin m ...
- 二十六个月Android学习工作总结【转】
原文:二十六个月Android学习工作总结 1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该 ...
- Vue v-for嵌套数据渲染问题
Vue v-for嵌套数据渲染问题 问题描述: 由于在获取商品子分类的时候,同时需要获取子分类下的商品,那么多层的列表渲染就只能是第一层好用 问题原因: vue在处理多层的渲染的时候,不能直接用等号赋 ...