ee-core: v5.0.0
Usage Instructions
The framework provides a unified cross module to implement cross-language support.
cross API
See documentation: API
Create Service
Creating a service is very simple. See the code below.
javascript
// File 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() {
// Service name, typically the language being used
const serviceName = "java";
// Jar package path, the getExtraResourcesDir method automatically handles the path before and after packaging.
const jarPath = path.join(getExtraResourcesDir(), 'java-app.jar');
const opt = {
// Program name
name: 'javaapp',
// Executable program path, or a local executable command
cmd: path.join(getExtraResourcesDir(), 'jre1.8.0_201/bin/javaw.exe'),
// Program directory, such as the directory containing the jar file
directory: getExtraResourcesDir(),
// Executable program arguments. If the configured port is occupied, the framework will randomly generate one.
args: ['-jar', '-server', '-Xms512M', '-Xmx512M', '-Xss512k', '-Dspring.profiles.active=prod', `-Dserver.port=18080`, `-Dlogging.file.path=${getLogDir()}`, `${jarPath}`],
// Whether to exit the Electron application when the program exits
appExit: false,
}
if (is.macOS()) {
// If the executable program differs across platforms, you can differentiate by operating system
opt.cmd = path.join(getExtraResourcesDir(), 'jre1.8.0_201/Contents/Home/bin/java');
}
if (is.linux()) {
// Same as above
}
// Run the program, return the cross process object
const entity = await cross.run(serviceName, opt);
// Program name
logger.info('server name:', entity.name);
// Program option configuration
logger.info('server config:', entity.config);
// Program service address
logger.info('server url:', entity.getUrl());
return;
}Start with Application Launch
If you want the Java service to start when the desktop application runs, you can import and call it directly in the preload module.
javascript
// File electron/preload/index.js
/*************************************************
** preload is a pre-load module, this file will be loaded when the program starts **
*************************************************/
const { crossService } = require('../service/cross');
function preload() {
// Call directly
crossService.createJavaServer();
}Get Service Address
Get the local service address by program name, typically ip:port (http://127.0.0.1:18080). If the configured port 18080 is occupied, the framework will randomly generate one.
javascript
/**
* Get service url
*/
async getUrl(args) {
const { name } = args;
const serverUrl = cross.getUrl(name);
return serverUrl;
}Kill Process
Kill a process by program name, or kill all processes.
javascript
/**
* 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;
}Communication
HTTP is the most universal communication protocol currently. IPC communication for different languages may be implemented in the future.
javascript
/**
* 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;
}