繁体   English   中英

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

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

如何使用电子实现后台服务。

我遇到了麻烦,谁能告诉我如何使用即使在关闭应用程序后仍能运行的电子启动后台服务。 我尝试了很多解决方案,但所有解决方案都在关闭应用程序后停止服务。

Electron 并非设计为在后台运行。 如果您正在关闭应用程序,那么它将终止与之相关的所有进程。 Electron 仅用于提供 GUI 层。 毕竟它是混合应用程序,它不与核心操作系统服务交互以像后台服务一样运行自己。

除此之外还有两种选择:

  1. 如果您使用其他东西编写服务,例如节点或 .net 应用程序,那么您可能可以使用 Electron 与该服务进行交互(通过捆绑节点访问 Windows API)。
  2. 创建系统托盘等功能。 将应用程序最小化到系统托盘。

参考链接

是的,可以使用电子进程 npm 库。 参考:- https://www.npmjs.com/package/electron-process

首先,您必须注册要在后台运行的模块,只需创建简单的 background.html,

--background.html-- 在脚本标签中添加以下几行,

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

在主进程中,只需创建一个浏览器窗口,您的 background.html 将在其中运行并将其保留为隐藏窗口,

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

希望它有所帮助,问候。

您可以使用托盘。 这是一个例子( 来源):

"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