业务
ee-bin: v1.5.0
ee-core: v2.9.2
# 调用说明
框架提供了统一的cross模块来实现跨语言支持。
# Cross API
请查看文档:API
# 创建服务
创建服务非常简单,参见下方代码。
// 文件 electron/service/cross.js
/**
* create python service
* In the default configuration, services can be started with applications.
* Developers can turn off the configuration and create it manually.
*/
async createPythonServer() {
// method 1: Use the default Settings
//const entity = await Cross.run(serviceName);
// method 2: Use custom configuration
const serviceName = "python";
const opt = {
name: 'pyapp',
cmd: path.join(Ps.getExtraResourcesDir(), 'py', 'pyapp'),
directory: path.join(Ps.getExtraResourcesDir(), 'py'),
args: ['--port=7074'],
windowsExtname: true,
appExit: true,
}
const entity = await Cross.run(serviceName, opt);
Log.info('server name:', entity.name);
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').createPythonServer();
}
# 获取服务地址
根据程序name
,获取本地服务地址,一般为 ip:port (http://127.0.0.1:7074)。如果配置中的端口7074被占用,则框架会随机生成一个。
/**
* 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;
}
上次更新: 2025/04/10, 03:07:49