当计算机(正在运行node.js的计算机)进入睡眠或关闭状态时,是否可以让OS X通知我的node.js应用程序?似乎应该如此,但是我还没找到任何与node.js相关的东西。我确实找到了这个,但这是在谈论可可应用。也许我可以让其他一些应用程序(例如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();
您可以在此处找到您可以观察到的其他类型的通知。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句