Node.js/OS X:如何在计算机进入睡眠状态时通知Node.js应用程序
Node.js / OS X: How to notify node.js app when computer enters sleep
当计算机(运行node.js的计算机)进入睡眠或关闭时,是否可以让OS X通知我的node.js应用程序?看起来应该是这样,但我一直找不到任何与node.js相关的东西。我确实发现了这个,但它说的是Cocoa应用程序。也许我可以让其他应用程序(比如Cocoa)接收睡眠通知并将其传递给node.js?
我很乐意听到任何可能为我指明正确方向的建议。
使用nodobjc
(它似乎有效,但我还没有很好地测试它):
'use strict';
const $ = require('nodobjc');
// Load the AppKit framework.
$.framework('AppKit');
// Create delegate that gets notified
let Delegate = $.NSObject.extend('Delegate');
// The function that gets called when OS X is going to sleep.
function receiveSleepNote(self, cmd, notif) {
console.log('going to sleep');
}
Delegate.addMethod('receiveSleepNote:', 'v@:@', receiveSleepNote);
Delegate.register();
// Instantiate the delegate and set it as observer.
let delegate = Delegate('alloc')('init');
let nc = $.NSWorkspace('sharedWorkspace')('notificationCenter');
nc(
'addObserver', delegate,
'selector' , 'receiveSleepNote:',
'name' , $.NSWorkspaceWillSleepNotification,
'object' , null
)
// Set up the runloop.
let app = $.NSApplication('sharedApplication');
function runLoop() {
let pool = $.NSAutoreleasePool('alloc')('init');
try {
app('nextEventMatchingMask', $.NSAnyEventMask.toString(),
'untilDate', $.NSDate('distantFuture'),
'inMode', $.NSDefaultRunLoopMode,
'dequeue', 1);
} catch(e) {
console.error('run loop error', e.message);
};
pool('drain');
process.nextTick(runLoop);
}
runLoop();
您可以在此处找到其他类型的通知。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 如何使用 node.js 比较两个 json 数组
- Node.js's Buffer.writeFloatBE in Javascript
- node.js:setInterval()正在跳过调用
- 如何处理node.js节点mongodb中的连接和查询队列
- Node.js正在更改应用程序以使用集群模块
- 如何使用Node.js最有效地解析网页
- 在openshift node js应用程序中获取请求
- 正在删除node.js中已验证的网站
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 在索引.html和应用.js [node.js] 之间共享变量
- Node.js中的JavaScript原型对象效率
- node.js请求数据事件未在CORS ajax调用中触发
- 使用node.js制作网站
- 使用node.js服务器中的evernote api获取note内容
- 有没有任何方法可以使用node-js从不同的机器打开浏览器
- 使用CI和CodeDeploy对node.js应用程序进行连续部署
- 在Node.js中上传和检索图像
- 什么's本地node.js服务器和python简单http服务器之间的区别
- 如何使用 Node JS 获取 JSON 嵌套数组值