簡體   English   中英

如何在電子角項目中使用永久監控器?

[英]How to use forever-monitor with Electron-Angular project?

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

我在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");
});

最后我從app.component.ts調用函數

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

我認為在資產目錄中設置腳本不是一個好主意。 我希望將其打包為額外的資源。

下一個代碼段將允許啟動您的節點進程

  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

那是這樣的:

1-將notification-process.js文件從assets文件夾移至主目錄。

2-更改了main.js中的文件路徑:

var child = new (forever.Monitor)(path.join(__dirname, 'notification-process.js')...

如果不使用join,則打包應用程序后將無法使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM