node-webkit教程(7)Platform Service之APP

文/玄魂

前言

几个月前,要开发一个简易的展示应用,要求支持离线播放(桌面应用)和在线播放(web应用)。

当时第一想到的是flex,同一套代码(或者只需少量的更改)就可以同时运行在桌面和浏览器上。由于很多展现效果要全新开发,我想到了impress.js(https://github.com/bartaz/impress.js/)。如果选择impress.js,就意味着要将html5作为桌面应用,当时想到要封装webkit,但是本人对这方面也不是很熟悉,时间也很有限,就又沿着这个方向搜索,找到了node-webkit(https://github.com/rogerwang/node-webkit)。

node-webkit解决了我通过htmljs来编写桌面应用的难题

至于node-webkit的定义,按照作者的说法:

“ 基于node.js和chromium的应用程序实时运行环境,可运行通过HTML(5)、CSS(3)、Javascript来编写的本地应用程序。node.js和webkit的结合体,webkit提供DOM操作,node.js提供本地化操作;且将二者的context完全整合,可在HTML代码中直接使用node.js的API。”

从本篇文章开始,为您介绍Platform Services些列的API,本系列由以下类别:

·             App – 每个应用运行时全局api

·             Clipboard – 剪贴板

·             Tray – 状态栏图标,消息通知

·             File dialogs-文件选择对话框

·             Shell – 桌面相关

·             Handling files and arguments-处理文件和相关参数

7.1  APP 概述

APP类别的API 是针对当前正在运行的应用程序实例的,换个说法是进程级别的(这样说还不准确,node-webkit每一个窗口在单独进程中,应用本身是多进程的)。这些API和程序的启动、关闭关系最密切。但是从目前文档中的API来看,APP类别的API显得不是很丰富。

新建appDemo.html和package.json文件。

package.json内容如下:

{

"name": "app-demo",

"main": "appDemo.html",

"nodejs":true,

"window": {

"title": "appDemo",

"toolbar": true,

"width": 800,

"height": 600,

"resizable":true,

"show_in_taskbar":true,

"frame":true,

"kiosk":false,

"icon": "2655716405282662783.png",

},

"webkit":{

"plugin":true

}

}

appDemo.html内容如下:

<html>

<head>

<title>appDemo</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body >

<h1>app api 测试</h1>

<script>

// Load native UI library

var gui = require('nw.gui');

var win = gui.Window.get();

</script>

</body>

</html>

7.1  获取APP对象

通过如下方式获得APP对象:

// Load native UI library

var gui = require('nw.gui');

var app = gui.App;

7.2 获取命令行参数

鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客(www.xuanhun521.com)

很多时候,我们启动程序需要从命令行输入参数,可以通过argv、fullArgv和filteredArgv获取输入参数。关于三者的区别参考:https://github.com/rogerwang/node-webkit/wiki/App#fullargv。我的测试结果和文档还是有出入的。

修改appDemo.html如下:

<html>

<head>

<title>appDemo</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body >

<h1>app api 测试</h1>

<script>

// Load native UI library

var gui = require('nw.gui');

var app = gui.App;

apendText(app.argv);

apendText(app.fullArgv);

apendText(app.filteredArgv);

function apendText(text)

{

var element = document.createElement('div');

element.appendChild(document.createTextNode(text));

document.body.appendChild(element);

}

</script>

</body>

</html>

在命令行启动程序:

运行结果如下:

7.3 dataPath

应用的数据存储目录,在不同的操作系统上路径不同,Windows: %LOCALAPPDATA%/<name>

Linux: ~/.config/<name>;

OSX:~/Library/Application Support/<name>

这里的<name>是在package.json中定义的name字段的值,所以需要在定义name值的时候保证全局唯一。

7.4 获取manifest

使用manifest属性,可以获取package.json中的json对象。修改appDemo。html的脚本内容如下:

<script>

// Load native UI library

var gui = require('nw.gui');

var app = gui.App;

var manifest = app.manifest;

apendText(manifest.name);

function apendText(text)

{

var element = document.createElement('div');

element.appendChild(document.createTextNode(text));

document.body.appendChild(element);

}

</script>

结果如下:

7.5 清除缓存

鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客(www.xuanhun521.com)

可以调用clearCache()方法,清除应用在内存和磁盘上的缓存。

7.6 关闭程序

关闭程序有两个函数可以调用,分别为closeAllWindows()和quit()方法,两者的区别在于closeAllWindows()方法会发送窗口的关闭消息,我们可以监听close事件(参考:http://www.xuanhun521.com/Blog/2014/4/14/node-webkit%E5%AD%A6%E4%B9%A04native-ui-api-%E4%B9%8Bwindow),阻止窗口关闭或者做其他日志等工作。quit()方法不会发送任何消息,直接退出程序。

7.7 Crash dump

从node-webkit  0.8.0版本开始,如果应用崩溃,一个minidump 文件会被保存到磁盘,用以调试和寻找程序崩溃的原因。默认情况下,dump文件保存在系统的临时文件夹中,我们也可以通过api来设置dump文件的存放目录。以下是个版本系统的临时目录:

·      Linux: /tmp

·      Windows: System temporary directory

·      Mac: ~/Library/Breakpad/product name (product name is defined in .plist file in the application bundle)

为了方便测试,node-webkit提供了App.crashBrowser()和App.crashRenderer()两个api,分别保存browser 进程和render进程的数据。下面我们通过实例演示将dump文件保存到本地磁盘D。

<script>

// Load native UI library

var gui = require('nw.gui');

var app = gui.App;

app.setCrashDumpDir('d:\\');//设置转储目录

app.crashBrowser();

app.crashRenderer();

function apendText(text)

{

var element = document.createElement('div');

element.appendChild(document.createTextNode(text));

document.body.appendChild(element);

}

</script>

运行程序,应用启动后会崩溃退出,在D盘会看到转储文件:

如何查看转储文件,这里就不详细介绍了,会在专门的文章中讲解,读者现在可以参考文档中的链接:

Decoding the stack trace

To extract the stack trace from the minidump file, you need the minidump_stackwalk tool, symbols file of node-webkit binary and the minidump (.dmp) file generated from the crash.

See http://www.chromium.org/developers/decoding-crash-dumps

http://code.google.com/p/google-breakpad/wiki/GettingStartedWithBreakpad

Symbols file of official node-webkit binary is provided staring from 0.8.0. It can be downloaded from:

Resources

Linux symbol files of breakpad

https://s3.amazonaws.com/node-webkit/v0.8.0/nw.breakpad.ia32.gz

https://s3.amazonaws.com/node-webkit/v0.8.0/nw.breakpad.x64.gz

windows pdb file

https://s3.amazonaws.com/node-webkit/v0.8.0/nw.exe.pdb.zip

mac dSYM files

https://s3.amazonaws.com/node-webkit/v0.8.0/node-webkit-osx-dsym-v0.8.0.tar.gz

7.8  获取代理

使用getProxyForURL(url),可以获得加载该url时使用的代理信息。返回值使用PAC格式(参考:http://en.wikipedia.org/wiki/Proxy_auto-config)。

7.6 小结

本文内容主要参考node-webkit的官方英文文档,做了适当的调整(https://github.com/rogerwang/node-webkit/wiki/App

https://github.com/rogerwang/node-webkit/wiki/Crash-dump)。

下一篇文章,介绍Clipboard。

鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客(www.xuanhun521.com)

更多相关内容,欢迎访问玄魂的博客(更多node-webkit相关内容 http://www.xuanhun521.com/Blog/Tag/node-webkit)

ps:nw.js,electron交流群 313717550

node-webkit教程(7)Platform Service之APP的更多相关文章

  1. node-webkit教程(11)Platform Service之shell

    node-webkit教程(11)Platform Service之shell 文/玄魂 目录 node-webkit教程(10)Platform Service之shell 前言 11.1  She ...

  2. node-webkit教程(10)Platform Service之File dialogs

    node-webkit教程(10)Platform Service之File dialogs 文/玄魂 目录 node-webkit教程(10)Platform Service之File dialog ...

  3. node-webkit教程(8)Platform Service之Clipboard

    node-webkit教程(8)Platform Service之Clipboard 文/玄魂 目录 node-webkit教程(8)Platform Service之Clipboard 前言 8.1 ...

  4. 10+ 最佳的 Node.js 教程和实例

    如果你正在找Node.js的学习资料及指南,那么请继续(阅读),我们的教程将会覆盖即时聊天应用.API服务编写.投票问卷应用.人物投票APP.社交授权. Node.js on Raspberry Pi ...

  5. node.js应用生成windows service的plugin——winser

    from:http://xiaomijsj.blog.163.com/blog/static/89685520135854036206/ 针对项目中windows server machine 不断重 ...

  6. Node.js 教程 01 - 简介、安装及配置

    系列目录: Node.js 教程 01 - 简介.安装及配置 Node.js 教程 02 - 经典的Hello World Node.js 教程 03 - 创建HTTP服务器 Node.js 教程 0 ...

  7. Node.js 教程 04 - 模块系统

    前言: Node.js的模块系统类似于C/C++的文件引用,可以声明对象,也可以定义类 创建对象. 大家这么理解,就简单了. 定义: 为了让Node.js的文件可以相互调用,Node.js提供了一个简 ...

  8. Node.js教程系列~目录

    Node.js这个东西在近几年火起来了,而且会一直火下去,无论在infoq还是在cnblogs,csdn上,都可以到处看到它的样子,它主推的应该就是异步式I/O 吧,是的,设计的很完美,很吸引人,虽然 ...

  9. android如何实现开机自动启动Service或app

    第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app. import and ...

随机推荐

  1. 关于Android开发手机连接不上电脑问题解决方案

    1.当然首先你得将手机里的usb debug选项选上,否则lsusb是不会有你的设备的2. lsusb 查看usb设备id3. sudo vim /etc/udev/rules.d/51-androi ...

  2. 共用字体-UI界面编辑器(SkinStudio)教程

    添加一个Label控件,设置好字体属性 再添加一个Label控件,字体属性还是默认的 只需要将字体属性的Name字段名称改为需要使用的字体属性的Name字段名称即可(如Label1使用的字体)

  3. SQL触发器中的deleted表和inserted表

    SQL触发器中的deleted表和inserted表 在触发器语句中用两个特殊的表一个是deleted表和inserted.它们是通过触发器操作自动创建驻留在内存中的临时表. 描述: Deleted表 ...

  4. 第三十七章 springboot+docker(手动部署)

    一.下载centos镜像 docker pull hub.c.163.com/library/centos:latest docker tag containId centos:7 docker ru ...

  5. linux ddos防御攻击

    Linux Ddos防御攻击 [root@lxh ~]# netstat -ntu |awk '{print $5}'|grep '[0-9]'|cut -d: -f1 |sort |uniq -c| ...

  6. oracle 驱动安装备忘

    ubuntu 从oracle官网下载两个必须的rpm包(这里选择的是version12.1.0.2.0, 64位操作系统) oracle-instantclient12.1-basic-12.1.0. ...

  7. python-->基础-->004-->迭代器

    http://blog.chinaunix.net/uid-23500957-id-3990473.html http://www.cnblogs.com/vamei/archive/2012/07/ ...

  8. each处理json数据

    eg:给传进来的ID中当其对应的值为true时,即给对应的ID标签添加一个class 名为  focus,如: var obj = { id01:'true', id02:'flase', id03: ...

  9. coredump调试的使用

    一,什么是coredump 跑程序的时候经常碰到SIGNAL 或者 call trace的问题,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止,并且在满 ...

  10. webapi返回json格式,并定义日期解析格式

    1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...