
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
基于 JS 实现的前端 Iframe 和 Worker 通信工具包
https://blog.csdn.net/time_____/article/details/130246659
应对多种场景下 Iframe 和 Worker 通信的诉求,主要应用于父对子,子对子之间一个或多个页面的通讯
npm install js-ipc
或
yarn add js-ipc
或
pnpm install js-ipc
import { Client, PeerToPeer, Server } from "js-ipc";
const { Client, PeerToPeer, Server } = require("js-ipc");
<script src="./node_modules/js-ipc/dist/umd/index.js"></script>
<script>
console.log(JsIPC);
</script>
详情见:example/super&child 目录
父页面
// 初始化
const server = new Server({ target: "#son" });
// 接收子页面消息
server.on("msg", console.log);
// 加载,加载完成
await server.mount().load();
// 发送消息给子页面
server.send({ type: "msg", data: { name: "parent" } });
子页面
// 初始化
const client = new Client({ target: window.parent });
// 接收父页面消息
client.on("msg", console.log);
// 发送消息给父页面
client.send({ type: "msg", data: { name: "son" } });
详情见:example/child&child 目录
子页面 1
// 初始化
const client = new Client({ target: window.parent });
// 接收消息
client.on("msg", console.log);
// 加载当前页面
client.mount();
// 等待页面全部加载完成后发送消息
client.on("load:finish", () => {
client.send({ type: "msg", data: { name: "son" } });
});
子页面 2
// 初始化
const client = new Client({ target: window.parent });
// 接收消息
client.on("msg", console.log);
// 加载当前页面
client.mount();
// 等待页面全部加载完成后发送消息
client.on("load:finish", () => {
client.send({ type: "msg", data: { name: "son2" } });
});
父页面
// 初始化
const peer = new PeerToPeer(["#son2", "#son"]);
// 连接子页面,并等待加载
await peer.connect().load();
// 加载完成后触发广播
peer.broadcast({ type: "load:finish" });
详情见:example/child&child&child 目录
// 与子<--->子页面一样,父页面需要传入多个 Iframe 元素
const peer = new PeerToPeer(["#son2", "#son3", "#son"]);
client.unMount();
client.reset();
父页面
const server = new Server({
target: "#son",
handlers: {
log: console.log,
},
});
子页面
// 调用父页面或其他页面的log函数 ,参数是 "log"
client.invokeHandler({ type: "log", data: ["log"] });
// 相同id可以通信
const server = new Server({
target: "#son",
id: 100,
});
const client = new Client({
target: window.parent,
id: 100,
});
const client1 = new Client({
target: window.parent,
});
// server 和 client 可以通信,client1 不行
详情见:example/super&child&child 目录 当父页面中包含多个子页面时,可以使用 PeerToPeer 中的广播功能
子页面 1
const client = new Client({
target: window.parent,
});
client.mount();
子页面 2
const client2 = new Client({
target: window.parent,
});
client2.mount();
父页面
const peer = new PeerToPeer(["#son", "#son2"]);
peer.connect();
// 发送广播
peer.broadcast({ type: "msg", data: { name: "parent" } });
// 发起函数调用
peer.invoke({ type: "son4info", data: ["parent"] });
和 iframe 相同,只需将 PeerToPeer 的参数改成 worker
const worker1 = new Worker("./worker1.js", { type: "module" });
const worker2 = new Worker("./worker2.js", { type: "module" });
const peer = new PeerToPeer([worker1, worker2]);
FAQs
JavaScript通信工具包,支持Iframe和Worker通信
The npm package js-ipc receives a total of 0 weekly downloads. As such, js-ipc popularity was classified as not popular.
We found that js-ipc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.