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

  • 基础功能

  • 生成软件

  • 升级

  • 跨语言支持

    • 介绍
    • go

    • java

      • 开始
      • 业务
      • python

    • 更新记录
    • 常见问题
    目录

    业务

    ee-core: v2.8.0

    # 调用说明

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

    # Cross API

    请查看文档:API

    # 创建服务

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

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

    # 跟随软件启动

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

    // 文件 electron/preload/index.js
    
    /*************************************************
     ** preload为预加载模块,该文件将会在程序启动时加载 **
     *************************************************/
    const Addon = require('ee-core/addon');
    const Services = require('ee-core/services');
    
    /**
     * 预加载模块入口
     */
    module.exports = async () => {
    
      // 直接调用
      Services.get('cross').createJavaServer();
    }
    

    # 获取服务地址

    根据程序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(args) {
        const { name, urlPath, params} = args;
        const hc = new HttpClient();
        const serverUrl = Cross.getUrl(name);
        console.log('Server Url:', serverUrl);
    
        const apiHello = serverUrl + urlPath;
        const options = {
          method: 'GET',
          data: params || {},
          dataType: 'json',
          timeout: 1000,  
        };
        const result = await hc.request(apiHello, options);
    
        return result.data;
      }
    
    上次更新: 2024/12/26, 10:28:21
    开始
    开始

    ← 开始 开始→

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