IPC API
安装
npm install @cutos/core
引入依赖
import {CoreAPI} from '@cutos/core';
CoreAPI.getIPC
获取IPC实例对象
let ipc = CoreAPI.getIPC([destAddress]);
- destAddress: 可选参数,目标IP地址,用于设备间进程通信
ipc.sendTo
发送IPC信息
ipc.sendTo(channel, args, callback = null);
- channel: 频道名称
- args: 发送的信息
- callback: 回调函数
ipc.on
监听IPC
ipc.on(channel, listener);
- channel: 频道名称
- listener: 监听函数 (args, callback)
本地设备通信举例:
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
let ipc = CoreAPI.getIPC();
//监听IPC信息
ipc.on("channel-1", function (args, callback) {
console.log(args);
});
//发送IPC信息
ipc.sendTo("channel-1", {event: "event-1", msg: "msg-1"});
- 返回结果示例:
{
"event": "event-1",
"msg": "msg-1"
}
设备间进程通信举例:
发送方
IP:192.168.1.136
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
//目标IP:192.168.1.149
let ipc = CoreAPI.getIPC("192.168.1.149");
//发送IPC信息
ipc.sendTo("channel-2", {event: "event-2", msg: "msg-2"}, (result) => {
if (!error) {
console.log(result)
}
});
- 返回结果示例:
"收到了"
接收方
IP:192.168.1.149
import {CoreAPI} from '@cutos/core';
CoreAPI.init();
//目标IP:192.168.1.136
let ipc = CoreAPI.getIPC("192.168.1.136");
//监听IPC信息
let ipcListener = function (args, context) {
console.log(args);
ipc._sendResponse(context, "收到了");
}
ipc.on("channel-2", ipcListener);
- 返回结果示例:
{
"event": "event-2",
"msg": "msg-2"
}