IPC API
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 send
- callback: callback function
ipc.on
Listen IPC
ipc.on(channel, listener);
- channel: channel name
- listener: listener function (args, context)
ipc.sendResponse
The receiver replies to the message
ipc.sendResponse(context, result, error = null);
- context: context information
- result: reply message content
- error: error message
Local device communication example:
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
let ipc = CoreAPI.getIPC();
//Listen to IPC information
ipc.on("channel-1", function (args, context) {
console.log(args);
});
//Send IPC information
ipc.sendTo("channel-1", {event: "event-1", msg: "msg-1"});
- Return result example:
{
"event": "event-1",
"msg": "msg-1"
}
Example of inter-device process communication:
Sender
IP:192.168.1.136
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
//Destination IP:192.168.1.149
let ipc = CoreAPI.getIPC("192.168.1.149");
//Send IPC information
ipc.sendTo("channel-2", {event: "event-2", msg: "msg-2"}, (result, error) => {
if (!error) {
console.log(result)
}
});
- Return result example:
"Received"
Receiver
IP:192.168.1.149
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
//Target IP:192.168.1.136
let ipc = CoreAPI.getIPC("192.168.1.136");
//Listen for IPC information
let ipcListener = function (args, context) {
console.log(args);
ipc.sendResponse(context, "Received");
}
ipc.on("channel-2", ipcListener);
- Return result example:
{
"event": "event-2",
"msg": "msg-2"
}