Nar*_*gis 8 node.js node-modules forever-monitor electron angular
我将Angular 2与Electron一起使用,并希望继续在后台运行进程以显示通知。我为此使用了永远的监视器,它只能在开发模式下工作,但是当我使用电子打包程序打包我的应用程序时,它将停止工作。我的代码如下所示:
主要
exports.runBackgroundProcess = () => {
// Run a background process forever
var forever = require('forever-monitor');
var child = new(forever.Monitor)('src/assets/notification-process.js',
{
env: {ELECTRON_RUN_AS_NODE: 1},
options: []
});
child.start();
}
Run Code Online (Sandbox Code Playgroud)
我在main.ts中编写了一个函数,当从角度组件中调用该函数时将运行后台进程。notification-process.js中的代码如下:
notification-process.js
notifier = require('node-notifier')
notifierFun = (msg) => {
notifier.notify({
title: 'Notify Me',
message: msg,
wait: true
});
}
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
notifierFun("Message from notification process");
});
Run Code Online (Sandbox Code Playgroud)
最后我从app.component.ts调用函数
let main_js = this.electronService.remote.require("./main.js");
main_js.runBackgroundProcess();
Run Code Online (Sandbox Code Playgroud)
这就是它的工作原理:
1-将notification-process.js文件从资产文件夹移动到主目录。
2-更改了 main.js 中的文件路径:
var child = new (forever.Monitor)(path.join(__dirname, 'notification-process.js')...
Run Code Online (Sandbox Code Playgroud)
不使用join的话,打包app后就不起作用了。
| 归档时间: |
|
| 查看次数: |
246 次 |
| 最近记录: |