gpt4 book ai didi

node.js - 如何在 Electron-Angular 项目中使用 forever-monitor?

转载 作者:太空狗 更新时间:2023-10-29 17:15:09 26 4
gpt4 key购买 nike

我将 Angular 2 与 Electron 一起使用,并希望继续在后台运行一个进程以显示通知。我正在使用 forever-monitor为此,它只能在开发模式下工作,但是当我使用 electron-packager 打包我的应用程序时,它就停止工作了。我的代码看起来像这样:

ma​​in.ts

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();
}

我在 main.ts 中写了一个函数,它会在从 Angular 组件调用时运行后台进程。 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");
});

最后我从 app.component.ts 调用函数

let main_js  = this.electronService.remote.require("./main.js");
main_js.runBackgroundProcess();

最佳答案

我不认为将脚本设置在 Assets 目录中是个好主意。我希望将其打包为额外资源。

下一个片段将允许启动您的 Node 进程

  var child_process = require('child_process');
var child = child_process.fork('notification-process.js',[],{
cwd : 'resources'
});

如果打包后还不行,可能是你的文件没有打包,打包成额外资源,修改package.json如下:这会将 webserver 文件夹打包到 resources/webserver 文件夹:

 "target": [
"win": {
"target": "nsis",
"icon": "build/icon.ico",
"extraResources" : [{
"from" : "webserver",
"to" : "webserver"}
]
},

供引用,看看: https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options

关于node.js - 如何在 Electron-Angular 项目中使用 forever-monitor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55536840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com