mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: use sharedworker for messages conn
This commit is contained in:
1 parent
b0f1c0d52c
commit
a2d5996311
8 files changed
+102
-13
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import ReconnectingWebsocket from 'reconnecting-websocket';
|
||||
|
||||
console.log('SharedWorker init');
|
||||
|
||||
const sharedConnections: Record<string, [ReconnectingWebsocket, MessagePort[]]> = {};
|
||||
interface RequestInitSharedConnPayload {
|
||||
type: 'sharedConn';
|
||||
cookie: any;
|
||||
path: string;
|
||||
}
|
||||
interface RequestSendMessagePayload {
|
||||
type: 'message';
|
||||
path: string;
|
||||
payload: string;
|
||||
}
|
||||
type RequestPayload = RequestInitSharedConnPayload | RequestSendMessagePayload;
|
||||
|
||||
function broadcastMsg(ports: MessagePort[], message: any) {
|
||||
for (const p of ports) p.postMessage(message);
|
||||
}
|
||||
function initSharedConn(path: string, port: MessagePort, cookie: any) {
|
||||
if (!sharedConnections[path]) {
|
||||
console.log('Init connection for', path);
|
||||
const sock = new ReconnectingWebsocket(path);
|
||||
const ports: MessagePort[] = [];
|
||||
sharedConnections[path] = [sock, ports];
|
||||
|
||||
sock.onopen = () => sock.send(cookie);
|
||||
sock.onmessage = (message) => {
|
||||
if (process.env.NODE_ENV !== 'production') console.log('SharedWorker.port.onmessage: ', message);
|
||||
const payload = JSON.parse(message.data);
|
||||
if (payload.event === 'auth') {
|
||||
if (['PermissionError', 'PrivilegeError'].includes(payload.error)) {
|
||||
broadcastMsg(ports, { type: 'close', path, error: payload.error });
|
||||
} else {
|
||||
console.log('Connected to', path);
|
||||
broadcastMsg(ports, { type: 'open', path });
|
||||
}
|
||||
} else broadcastMsg(ports, { type: 'message', path, payload: message.data });
|
||||
};
|
||||
sock.onerror = () => broadcastMsg(ports, { type: 'error', path });
|
||||
sock.onclose = (ev) => broadcastMsg(ports, { type: 'close', path, error: ev.reason });
|
||||
}
|
||||
|
||||
sharedConnections[path][1].push(port);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-undef
|
||||
onconnect = function (e) {
|
||||
const port = e.ports[0];
|
||||
|
||||
port.addEventListener('message', (msg: { data: RequestPayload }) => {
|
||||
if (msg.data.type === 'sharedConn') initSharedConn(msg.data.path, port, msg.data.cookie);
|
||||
if (msg.data.type === 'message') sharedConnections[msg.data.path]?.[0].send(msg.data.payload);
|
||||
});
|
||||
|
||||
port.start();
|
||||
};
|
||||
Reference in new issue
Block a user