通讯操作

This commit is contained in:
2025-01-03 17:09:16 +08:00
parent 9876c1cbcb
commit c4b47d117c
13 changed files with 419 additions and 169 deletions

View File

@ -2,17 +2,20 @@ import EventEmitter from "eventemitter3";
import { connect } from "./client";
import { ApiFunction, Server } from "./server";
export type SubscribeCallback = (message: any) => void;
export class Broker {
constructor() {}
// 订阅
async subscribe(topic: string, handler: (message: any) => void) {
async subscribe(topic: string, handler: SubscribeCallback): Promise<chrome.runtime.Port> {
const con = await connect("messageQueue", { action: "subscribe", topic });
con.onMessage.addListener((msg: { action: string; topic: string; message: any }) => {
if (msg.action === "message") {
handler(msg.message);
}
});
return con;
}
// 发布