简体   繁体   English

如何在电子 js 中运行后台服务?

[英]how to run a background service in electron js?

How to implement background service using electron.如何使用电子实现后台服务。

i'm having a trouble can anyone tell me how to start a background service using electron which runs even after closing the app.我遇到了麻烦,谁能告诉我如何使用即使在关闭应用程序后仍能运行的电子启动后台服务。 i have tried many solutions but all of them stop the service after closing the app.我尝试了很多解决方案,但所有解决方案都在关闭应用程序后停止服务。

Electron is not designed to run in background. Electron 并非设计为在后台运行。 If you are closing application then it will terminate all processes related with it.如果您正在关闭应用程序,那么它将终止与之相关的所有进程。 Electron is only used to provide GUI layer. Electron 仅用于提供 GUI 层。 After all it is hybrid application and it doesn't interact with core OS services to live itself like background service.毕竟它是混合应用程序,它不与核心操作系统服务交互以像后台服务一样运行自己。

Apart from this there are two options:除此之外还有两种选择:

  1. If you write a service with something else, say a node or .net application, then you probably could use Electron to interact with that service (via bundled Node accessing Windows APIs).如果您使用其他东西编写服务,例如节点或 .net 应用程序,那么您可能可以使用 Electron 与该服务进行交互(通过捆绑节点访问 Windows API)。
  2. Create feature like system tray.创建系统托盘等功能。 Minimise application to system tray.将应用程序最小化到系统托盘。

Ref Link 参考链接

Yes, it is possible by using electron-process npm library.是的,可以使用电子进程 npm 库。 ref :- https://www.npmjs.com/package/electron-process参考:- https://www.npmjs.com/package/electron-process

First you will have to register the module which you want to run in background, just create simple background.html,首先,您必须注册要在后台运行的模块,只需创建简单的 background.html,

--background.html-- add below lines in script tag, --background.html-- 在脚本标签中添加以下几行,

const background = require('electron-process').background;      
background.registerModule(require('../main/snippets/SnippetsManager'));

In main process just create one browser window in which your background.html will run and keep it as hidden window,在主进程中,只需创建一个浏览器窗口,您的 background.html 将在其中运行并将其保留为隐藏窗口,

--main.js-- --main.js--

app.once("ready", ev => {
    service = new BrowserWindow({
        width: 80, height: 60, center: true, minimizable: false, show: false,
        webPreferences: {
            nodeIntegration: false,
            webSecurity: true,
            sandbox: true,
        },
    });
    service.loadURL("file://' + __dirname + '/background.html");
    service.on("close", ev => {
        ev.sender.hide();
        ev.preventDefault(); // prevent quit process
    });
});

Hope it helped, Regards.希望它有所帮助,问候。

You can use tray.您可以使用托盘。 here is an example ( source ):这是一个例子( 来源):

"use strict";

// [run the app]
// $ npm install electron
// $ ./node_modules/.bin/electron .

const {app, nativeImage, Tray, Menu, BrowserWindow} = require("electron");

let top = {}; // prevent gc to keep windows

app.once("ready", ev => {
    top.win = new BrowserWindow({
        width: 800, height: 600, center: true, minimizable: false, show: false,
        webPreferences: {
            nodeIntegration: false,
            webSecurity: true,
            sandbox: true,
        },                                
    });
    top.win.loadURL("https://google.com/");
    top.win.on("close", ev => {
        //console.log(ev);
        ev.sender.hide();
        ev.preventDefault(); // prevent quit process
    });

    // empty image as transparent icon: it can click
    // see: https://electron.atom.io/docs/api/tray/
    top.tray = new Tray(nativeImage.createEmpty());
    const menu = Menu.buildFromTemplate([
        {label: "Actions", submenu: [
            {label: "Open Google", click: (item, window, event) => {
                //console.log(item, event);
                top.win.show();
            }},
        ]},
        {type: "separator"},
        {role: "quit"}, // "role": system prepared action menu
    ]);
    top.tray.setToolTip("hello electrol");
    //top.tray.setTitle("Tray Example"); // macOS only
    top.tray.setContextMenu(menu);

    // Option: some animated web site to tray icon image
    // see: https://electron.atom.io/docs/tutorial/offscreen-rendering/
    top.icons = new BrowserWindow({
        show: false, webPreferences: {offscreen: true}});
    top.icons.loadURL("https://trends.google.com/trends/hottrends/visualize");
    top.icons.webContents.on("paint", (event, dirty, image) => {
        if (top.tray) top.tray.setImage(image.resize({width: 16, height: 16}));
    });
});
app.on("before-quit", ev => {
    // BrowserWindow "close" event spawn after quit operation,
    // it requires to clean up listeners for "close" event
    top.win.removeAllListeners("close");
    // release windows
    top = null;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM