mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
fix UiContext.ws_prefix
This commit is contained in:
2 files changed
+7
-6
No files matched your search
@@ -59,14 +59,14 @@ const onmessage = (msg) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
const initWorkerMode = () => {
|
||||
const initWorkerMode = (endpoint) => {
|
||||
console.log('Messages: using SharedWorker');
|
||||
const worker = new SharedWorker(new URL('./worker.ts', import.meta.url), { name: 'HydroMessagesWorker' });
|
||||
worker.port.start();
|
||||
window.addEventListener('beforeunload', () => {
|
||||
worker.port.postMessage({ type: 'unload' });
|
||||
});
|
||||
worker.port.postMessage({ type: 'conn', cookie: document.cookie });
|
||||
worker.port.postMessage({ type: 'conn', path: endpoint, cookie: document.cookie });
|
||||
worker.port.onmessage = async (message) => {
|
||||
if (process.env.NODE_ENV !== 'production') console.log('onmessage: ', message);
|
||||
const { payload, type } = message.data;
|
||||
@@ -93,7 +93,7 @@ const messagePage = new AutoloadPage('messagePage', (pagename) => {
|
||||
const endpoint = url.toString().replace('http', 'ws');
|
||||
if (window.SharedWorker) {
|
||||
try {
|
||||
initWorkerMode();
|
||||
initWorkerMode(url);
|
||||
return;
|
||||
} catch (e) {
|
||||
console.error('SharedWorker init fail: ', e.message);
|
||||
|
||||
@@ -13,6 +13,7 @@ const ports: Set<MessagePort> = new Set();
|
||||
interface RequestInitSharedConnPayload {
|
||||
type: 'conn';
|
||||
cookie: string;
|
||||
path: string;
|
||||
}
|
||||
interface RequestAckPayload {
|
||||
type: 'ack';
|
||||
@@ -52,12 +53,12 @@ function onMessage(payload: any) {
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
function initConn(port: MessagePort, cookie: any) {
|
||||
function initConn(path: string, port: MessagePort, cookie: any) {
|
||||
ports.add(port);
|
||||
console.log('Init connection');
|
||||
lcookie = cookie.split('sid=')[1].split(';')[0];
|
||||
if (conn) return;
|
||||
const url = new URL('/websocket', location.origin);
|
||||
const url = new URL(path, location.origin);
|
||||
conn = new ReconnectingWebsocket(url.toString().replace('http', 'ws'));
|
||||
conn.onopen = () => {
|
||||
console.log('Connected');
|
||||
@@ -91,7 +92,7 @@ self.onconnect = function (e) {
|
||||
const port = e.ports[0];
|
||||
|
||||
port.addEventListener('message', (msg: { data: RequestPayload }) => {
|
||||
if (msg.data.type === 'conn') initConn(port, msg.data.cookie);
|
||||
if (msg.data.type === 'conn') initConn(msg.data.path, port, msg.data.cookie);
|
||||
if (msg.data.type === 'ack') ack[msg.data.id]?.();
|
||||
if (msg.data.type === 'unload') ports.delete(port);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user