npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-ipc

v2.0.4

Published

JavaScript通信工具包,支持Iframe和Worker通信

Downloads

3

Readme

js-ipc

介绍

基于 JS 实现的前端 Iframe 和 Worker 通信工具包

博客

https://blog.csdn.net/time_____/article/details/130246659

描述

应对多种场景下 Iframe 和 Worker 通信的诉求,主要应用于父对子,子对子之间一个或多个页面的通讯

调试说明

  1. pnpm i 安装依赖
  2. pnpm debug 调试
  3. pnpm build 编译构建
  4. pnpm server:hot 热运行 example
  5. pnpm build:hot 热构建
  6. 使用 live-server 打开 example 中的 html 进行操作

参与贡献

  1. Fork 本仓库
  2. Star 仓库

使用说明

安装

npm install js-ipcyarn add js-ipcpnpm install js-ipc

引入依赖

ESM

import { Client, PeerToPeer, Server } from "js-ipc";

CJS

const { Client, PeerToPeer, Server } = require("js-ipc");

浏览器中

<script src="./node_modules/js-ipc/dist/umd/index.js"></script>
<script>
  console.log(JsIPC);
</script>

基础用法

1. 父<--->子页面

详情见: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" } });

2.子<--->子页面

详情见: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" });

其他用法

1.子<--->子<--->子页面

详情见:example/child&child&child 目录

// 与子<--->子页面一样,父页面需要传入多个 Iframe 元素
const peer = new PeerToPeer(["#son2", "#son3", "#son"]);

2.卸载页面

client.unMount();

3.重置

client.reset();

4.调用函数

父页面

const server = new Server({
  target: "#son",
  handlers: {
    log: console.log,
  },
});

子页面

// 调用父页面或其他页面的log函数 ,参数是 "log"
client.invokeHandler({ type: "log", data: ["log"] });

5.索引 id 标识

// 相同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 不行

6.子<--->父<--->子页面

详情见: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"] });

7.线程通信

和 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]);