简单易用的 electron ipc 通信实现

·  阅读 320

img img

简单易用的 electron ipc 通信实现。

Install

$ yarn add electron-easy-ipc复制代码

Usage

数据结构返回:

{
  code: number;
  message: any;
}复制代码

renderer 单向通信:

// main
const { simpleOn, OK } = require("../lib/index");
simpleOn("simple-on", (options) => {
  const response = {
    code: OK,
    message: options
  }
  return Promise.resolve(response);
});复制代码
// renderer
const { longSend } = require("../lib/index");
const simples = {
  simple: "simple"
}

simpleSend("simple-on", simples).then((response) => {
  console.log(response);
});复制代码

long 双向通信:

// main
const { longOn } = require("../lib/index");
longOn("long-on", (options, server) => {
  // open
    server.onMessage = (message) => {
      console.log(message);
      // renderer send message to main
      // handler and use sendMessage send result message
      // server.sendMessage("main to renderer icepy");
    }
  //
  return () => {
    // disconnect
  }
});复制代码
// renderer
const { longSend } = require("../lib/index");
const initLongs = {
  long: "long"
}

const longClient = longSend("long-on", initLongs);
longClient.sendMessage("init renderer");
longClient.onMessage = (message) => {
  // longClient.sendMessage()
  longClient.sendMessage("renderer to main message");
}复制代码

LICENSE

MIT License Copyright (c) 2019 子曰五溪

分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改