IPC API
Installation
npm install @cutos/core
Introduce dependencies
import {CoreAPI} from '@cutos/core';
CoreAPI.getIPC
Get IPC instance object
let ipc = CoreAPI.getIPC([destAddress]);
- destAddress: Optional parameter, target IP address, used for inter-device process communication
ipc.sendTo
Send IPC information
ipc.sendTo(channel, args, callback = null);
- channel: channel name
- args: information to be sent
- callback: callback function
ipc.on
Monitor IPC
ipc.on(channel, listener);
- channel: channel name
- listener: listener function (args, respond)
Local device communication example:
Example 1 (without return value):
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
let channel1 = 'channel1';
let ipc = CoreAPI.getIPC();
//Listen for IPC information
ipc.on(channel1, (args) => {
console.log('on', channel1, 'received', args);
})
//Send IPC information
ipc.sendTo(channel1, 'no callback');
- Return result example:
on channel1 received: "no callback"
Example 2 (with return value):
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
let channel2 = 'channel2';
let ipc = CoreAPI.getIPC();
//Monitor IPC information
ipc.on(channel2, (args, respond) => {
console.log('on ' + channel2 + ' received: ' + JSON.stringify(args));
respond({sum: args.opt1 + args.opt2})
})
//Sending IPC information
ipc.sendTo(channel2, {opt1: 2, opt2: 2}, (result) => {
console.log('on ' + channel2 + ' callback: ' + JSON.stringify(result))
});
- Return result example:
on channel2 received: {"opt1":2,"opt2":2}
on channel2 callback: {"sum":4}
Example of inter-device process communication:
Sender (IP: 192.168.1.136)
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
let channel1 = 'channel1';
//Destination IP: 192.168.1.149
let ipc = CoreAPI.getIPC("192.168.1.149");
//Send IPC information
ipc.sendTo(channel1, 'no callback');
Receiver (IP: 192.168.1.149)
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
let channel1 = 'channel1';
let ipc = CoreAPI.getIPC();
//Listen for IPC information
let ipcListener = function (args) {
console.log('on', channel1, 'received', args);
}
ipc.on(channel1, ipcListener);
- Return result example:
on channel1 received: "no callback"