连接包
This commit is contained in:
parent
d0d1793786
commit
93d41169e1
@ -19,7 +19,8 @@
|
|||||||
"i18next": "^23.16.4",
|
"i18next": "^23.16.4",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
"react-i18next": "^15.1.0"
|
"react-i18next": "^15.1.0",
|
||||||
|
"uuid": "^11.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/compat": "^1.2.0",
|
"@eslint/compat": "^1.2.0",
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
import EventEmitter from 'eventemitter3';
|
import EventEmitter from "eventemitter3";
|
||||||
import { Connect } from '.';
|
import { IConnect, IServer } from ".";
|
||||||
|
|
||||||
export class ExtServer {
|
export class ExtServer implements IServer {
|
||||||
private EE: EventEmitter;
|
private EE: EventEmitter;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.EE = new EventEmitter();
|
this.EE = new EventEmitter();
|
||||||
chrome.runtime.onConnect.addListener((port) => {
|
chrome.runtime.onConnect.addListener((port) => {
|
||||||
this.EE.emit('connect', new ExtConnect(port));
|
this.EE.emit("connect", port.name, new ExtConnect(port));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onConnect(callback: (con: Connect) => void) {
|
onConnect(callback: (eventName: string, con: IConnect) => void) {
|
||||||
this.EE.on('connect', callback);
|
this.EE.on("connect", callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ export function connect() {
|
|||||||
return new ExtConnect(chrome.runtime.connect());
|
return new ExtConnect(chrome.runtime.connect());
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ExtConnect implements Connect {
|
export class ExtConnect implements IConnect {
|
||||||
private EE: EventEmitter;
|
private EE: EventEmitter;
|
||||||
private port: chrome.runtime.Port;
|
private port: chrome.runtime.Port;
|
||||||
|
|
||||||
@ -28,10 +28,10 @@ export class ExtConnect implements Connect {
|
|||||||
this.EE = new EventEmitter();
|
this.EE = new EventEmitter();
|
||||||
this.port = port;
|
this.port = port;
|
||||||
port.onMessage.addListener((message) => {
|
port.onMessage.addListener((message) => {
|
||||||
this.EE.emit('message', message);
|
this.EE.emit("message", message);
|
||||||
});
|
});
|
||||||
port.onDisconnect.addListener(() => {
|
port.onDisconnect.addListener(() => {
|
||||||
this.EE.emit('disconnect');
|
this.EE.emit("disconnect");
|
||||||
this.EE.removeAllListeners();
|
this.EE.removeAllListeners();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -41,11 +41,11 @@ export class ExtConnect implements Connect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMessage(callback: (message: unknown) => void) {
|
onMessage(callback: (message: unknown) => void) {
|
||||||
this.EE.on('message', callback);
|
this.EE.on("message", callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
onDisconnect(callback: () => void) {
|
onDisconnect(callback: () => void) {
|
||||||
this.EE.on('disconnect', callback);
|
this.EE.on("disconnect", callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
disconnect() {
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import EventEmitter from 'eventemitter3';
|
import EventEmitter from "eventemitter3";
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
|
||||||
export interface Server {
|
export interface IServer {
|
||||||
onConnect: (callback: (con: Connect) => void) => void;
|
onConnect: (callback: (eventName: string, con: IConnect) => void) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Connect {
|
export interface IConnect {
|
||||||
postMessage: (message: unknown) => void;
|
postMessage: (message: unknown) => void;
|
||||||
onMessage: (callback: (message: unknown) => void) => void;
|
onMessage: (callback: (message: unknown) => void) => void;
|
||||||
onDisconnect: (callback: () => void) => void;
|
onDisconnect: (callback: () => void) => void;
|
||||||
@ -13,51 +13,83 @@ export interface Connect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 消息通道, 通过连接封装消息通道
|
// 消息通道, 通过连接封装消息通道
|
||||||
export class MessageChannel {
|
export class Server {
|
||||||
private EE: EventEmitter;
|
private EE: EventEmitter;
|
||||||
// 实例id
|
|
||||||
private id: string;
|
|
||||||
|
|
||||||
constructor(private connect: Connect) {
|
constructor(private connect: IServer) {
|
||||||
this.EE = new EventEmitter();
|
this.EE = new EventEmitter();
|
||||||
this.id = uuidv4();
|
this.connect.onConnect((eventName, con) => {
|
||||||
connect.onMessage((message) => {
|
this.EE.emit(eventName, con);
|
||||||
// 判断消息是否为连接消息
|
|
||||||
if (message && typeof message === 'object') {
|
|
||||||
const data = message as { type: string; id: string };
|
|
||||||
// 判断实例
|
|
||||||
if (data.id == this.id) {
|
|
||||||
switch (data.type) {
|
|
||||||
case 'connect':
|
|
||||||
// 建立连接
|
|
||||||
break;
|
|
||||||
case 'message':
|
|
||||||
// 发送消息
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送一次消息并接收结果
|
on(eventName: string, callback: (con: IConnect) => void) {
|
||||||
async send(message: unknown) {
|
this.EE.on(eventName, callback);
|
||||||
return new Promise((resolve) => {
|
}
|
||||||
const callback = (message: unknown) => {
|
}
|
||||||
this.connect.onMessage(callback);
|
|
||||||
resolve(message);
|
export class Connect {
|
||||||
};
|
private EE: EventEmitter;
|
||||||
this.connect.onMessage(callback);
|
|
||||||
this.connect.postMessage(message);
|
constructor(
|
||||||
});
|
private id: string | IConnect,
|
||||||
}
|
private con: IConnect
|
||||||
|
) {
|
||||||
// 发送消息
|
this.EE = new EventEmitter();
|
||||||
postMessage(message: unknown) {
|
if (arguments.length === 1) {
|
||||||
this.connect.postMessage({
|
this.con = id as IConnect;
|
||||||
type: 'message',
|
this.con.onMessage((message) => {
|
||||||
id: this.id,
|
const data = message as { eventName: string; data: unknown[]; messageId: string };
|
||||||
data: message,
|
data.data.push(this.callbackFunc(data.messageId));
|
||||||
});
|
this.EE.emit(data.eventName, ...data.data);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 子连接
|
||||||
|
this.con.onMessage((message) => {
|
||||||
|
const data = message as { eventName: string; data: unknown; id: string };
|
||||||
|
if (data.eventName === "subcon") {
|
||||||
|
if (data.id !== this.id) return;
|
||||||
|
this.messageHandler(data.data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.con.onDisconnect(() => {
|
||||||
|
this.EE.emit("disconnect");
|
||||||
|
this.EE.removeAllListeners();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private callbackFunc(msgId: string): (...data: unknown[]) => void {
|
||||||
|
return function (...data: unknown[]) {
|
||||||
|
this.con.postMessage({ eventName: "callback", data, messageId: msgId });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private messageHandler(data: unknown) {
|
||||||
|
const subData = data.data as { eventName: string; data: unknown[]; messageId: string };
|
||||||
|
subData.data.push(this.callbackFunc(subData.messageId));
|
||||||
|
this.EE.emit(subData.eventName, ...subData.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
on(eventName: string, callback: (message: unknown) => void) {
|
||||||
|
this.EE.on(eventName, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
send(eventName: string, ...data: unknown[]) {
|
||||||
|
this.con.postMessage({ eventName, data });
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(eventName: string, ...data: unknown[]) {
|
||||||
|
// 判断最后一个参数是否为函数
|
||||||
|
const callback = data.pop();
|
||||||
|
if (typeof callback !== "function") {
|
||||||
|
data.push(callback);
|
||||||
|
}
|
||||||
|
this.con.postMessage({ eventName, data, messageId: uuidv4() });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 子连接
|
||||||
|
connect() {
|
||||||
|
return new Connect(uuidv4(), this.con);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@ -26,6 +26,9 @@ importers:
|
|||||||
react-i18next:
|
react-i18next:
|
||||||
specifier: ^15.1.0
|
specifier: ^15.1.0
|
||||||
version: 15.1.0(i18next@23.16.4)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
version: 15.1.0(i18next@23.16.4)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||||
|
uuid:
|
||||||
|
specifier: ^11.0.3
|
||||||
|
version: 11.0.3
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@eslint/compat':
|
'@eslint/compat':
|
||||||
specifier: ^1.2.0
|
specifier: ^1.2.0
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { ExtServer } from "@Packages/message/extension";
|
import { ExtServer } from "@Packages/message/extension";
|
||||||
|
import { Server } from "@Packages/message";
|
||||||
|
|
||||||
async function setupOffscreenDocument() {
|
async function setupOffscreenDocument() {
|
||||||
// 创建运行后台脚本的沙盒环境
|
// 创建运行后台脚本的沙盒环境
|
||||||
@ -20,7 +21,11 @@ async function main() {
|
|||||||
// 初始化沙盒环境
|
// 初始化沙盒环境
|
||||||
await setupOffscreenDocument();
|
await setupOffscreenDocument();
|
||||||
// 监听消息
|
// 监听消息
|
||||||
new ExtServer();
|
const extServer = new ExtServer();
|
||||||
|
const server = new Server(extServer);
|
||||||
|
server.on("", (con) => {
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user