electron-egg electron-egg
首页
  • v4.x
  • v3.x
  • v2.x
插件
  • v4.x
  • v3.x
demo
支持
知识点
案例
交流
  • GitHub (opens new window)
  • Gitee (opens new window)
首页
  • v4.x
  • v3.x
  • v2.x
插件
  • v4.x
  • v3.x
demo
支持
知识点
案例
交流
  • GitHub (opens new window)
  • Gitee (opens new window)
❤️成为赞助商
  • 快速入门

  • 基础功能

  • 生成软件

  • 升级

  • 跨语言支持

    • 介绍
    • go

    • java

      • 开始
      • 开发
      • python

    • 从v3升级v4
    • 更新记录
    • 常见问题
    目录

    开发

    ee-core: v2.8.0

    # 调用说明

    框架提供了统一的cross模块来实现跨语言支持。

    # cross API

    请查看文档:API

    # 创建服务

    创建服务非常简单,参见下方代码。

      // 文件 electron/service/cross.js
      const { cross } = require('ee-core/cross');
      const { getExtraResourcesDir, getLogDir } = require('ee-core/ps');
      const { is } = require('ee-core/utils');
    
      /**
       * create java server
       */
      async createJavaServer() {
        // 服务名称,一般填写使用的语言
        const serviceName = "java";
        // jar包路径,getExtraResourcesDir方法会自动处理打包前和打包后路径。
        const jarPath = path.join(getExtraResourcesDir(), 'java-app.jar');
        const opt = {
          // 程序名称
          name: 'javaapp',
          // 可执行程序路径,或本机的可执行命令
          cmd: path.join(getExtraResourcesDir(), 'jre1.8.0_201/bin/javaw.exe'),
          // 程序目录,如jar文件所在目录
          directory: getExtraResourcesDir(),
          // 可执行程序参数,如果配置中的端口被占用,则框架会随机生成一个。
          args: ['-jar', '-server', '-Xms512M', '-Xmx512M', '-Xss512k', '-Dspring.profiles.active=prod', `-Dserver.port=18080`, `-Dlogging.file.path=${getLogDir()}`, `${jarPath}`],
          // 程序退出时,是否退出electron应用
          appExit: false,
        }
        if (is.macOS()) {
          // 如果可执行程序多平台不一致,可根据操作系统来区分
          opt.cmd = path.join(getExtraResourcesDir(), 'jre1.8.0_201/Contents/Home/bin/java');
        }
        if (is.linux()) {
          // 同上
        }
    
        // 运行程序,返回cross进程对象
        const entity = await cross.run(serviceName, opt);
        // 程序名称
        logger.info('server name:', entity.name);
        // 程序option配置
        logger.info('server config:', entity.config);
        // 程序服务地址
        logger.info('server url:', entity.getUrl());
    
        return;
      }
    

    # 跟随软件启动

    如果你希望桌面软件运行时就启动java服务,那么可以在预加载模块,直接引入并调用。

    // 文件 electron/preload/index.js
    
    /*************************************************
     ** preload为预加载模块,该文件将会在程序启动时加载 **
     *************************************************/
    const { crossService } = require('../service/cross');
    
    function preload() {
      // 直接调用
      crossService.createJavaServer();
    }
    
    /**
    * 预加载模块入口
    */
    module.exports = {
      preload
    }
    

    # 获取服务地址

    根据程序name,获取本地服务地址,一般为 ip:port (http://127.0.0.1:18080)。如果配置中的端口18080被占用,则框架会随机生成一个。

      /**
       * Get service url
       */  
      async getUrl(args) {
        const { name } = args;
        const serverUrl = cross.getUrl(name);
        return serverUrl;
      }
    

    # kill进程

    通过程序name,kill进程,或kill所有进程。

      /**
       * kill service
       * By default (modifiable), killing the process will exit the electron application.
       */  
      async killServer(args) {
        const { type, name } = args;
        if (type == 'all') {
          cross.killAll();
        } else {
          cross.killByName(name);
        }
    
        return;
      }
    

    # 通信

    http是当前最通用的通信协议,后续可能为不同语言实现ipc通信。

      /**
       * Access the api for the cross service
       */
      async requestApi(name, urlPath, params) {
        const serverUrl = cross.getUrl(name);
        const apiHello = serverUrl + urlPath;
        console.log('Server Url:', serverUrl);
    
        const response = await axios({
          method: 'get',
          url: apiHello,
          timeout: 1000,
          params,
          proxy: false,
        });
        if (response.status == 200) {
          const { data } = response;
          return data;
        }
    
        return null;
      } 
    
    上次更新: 2025/06/06, 07:21:49
    开始
    开始

    ← 开始 开始→

    Theme by Vdoing | Copyright © 2023-2025 哆啦好梦 | 京ICP备15041380号-2
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×