aa
Some checks failed
test / Run tests (push) Failing after 11s
build / Build (push) Failing after 16s

This commit is contained in:
王一之 2025-02-12 23:12:03 +08:00
parent 3a41bf0f21
commit fcf9f6fd63
3 changed files with 4 additions and 8 deletions

View File

@ -14,7 +14,6 @@ export class Broker {
LoggerCore.getInstance().logger({ service: "messageQueue" }).debug("subscribe", { topic }); LoggerCore.getInstance().logger({ service: "messageQueue" }).debug("subscribe", { topic });
const con = await this.msg.connect({ action: "messageQueue", data: { action: "subscribe", topic } }); const con = await this.msg.connect({ action: "messageQueue", data: { action: "subscribe", topic } });
con.onMessage((msg: { action: string; topic: string; message: any }) => { con.onMessage((msg: { action: string; topic: string; message: any }) => {
console.log(msg);
if (msg.action === "message") { if (msg.action === "message") {
handler(msg.message); handler(msg.message);
} }

View File

@ -36,7 +36,6 @@ export class WindowMessage implements Message {
) { ) {
// 监听消息 // 监听消息
this.source.addEventListener("message", (e) => { this.source.addEventListener("message", (e) => {
console.log(e);
if (e.source === this.target || e.source === this.source) { if (e.source === this.target || e.source === this.source) {
this.messageHandle(e.data, new WindowPostMessage(this.target)); this.messageHandle(e.data, new WindowPostMessage(this.target));
} }
@ -169,11 +168,10 @@ export class ServiceWorkerMessageSend implements MessageSend {
constructor() {} constructor() {}
async init() { async init() {
const list = await clients.matchAll(); const list = await self.clients.matchAll({ includeUncontrolled: true, type: "window" });
this.target = list[0]; this.target = list[0];
console.log(this.target); self.addEventListener("message", (e) => {
addEventListener("message", (e) => { console.log("serviceWorker", e);
console.log(e);
this.messageHandle(e.data); this.messageHandle(e.data);
}); });
} }

View File

@ -87,12 +87,11 @@ export default class GMApi {
} }
} }
export async function sendMessageToOffscreen(action: string, data?: any) { export async function sendMessageToOffscreen(action: string, data?: any) {
// service_worker和offscreen同时监听消息,会导致消息被两边同时接收,但是返回结果时会产生问题,导致报错 // service_worker和offscreen同时监听消息,会导致消息被两边同时接收,但是返回结果时会产生问题,导致报错
// 不进行监听的话又无法从service_worker主动发送消息 // 不进行监听的话又无法从service_worker主动发送消息
// 所以这里通过clients.matchAll()获取到所有的client,然后通过postMessage发送消息 // 所以这里通过clients.matchAll()获取到所有的client,然后通过postMessage发送消息
const list = await clients.matchAll(); const list = await clients.matchAll({ includeUncontrolled: true, type: "window" });
list[0].postMessage({ list[0].postMessage({
type: "sendMessage", type: "sendMessage",
data: { action, data }, data: { action, data },