Files
NapCatQQ/.pnpm-store/v3/files/0d/a129bd28ba58e53bd63c76feb68507b96408ad43b96f6e2b44855bda3ee635faef05d98d8d7afa882b25a1979c5eedb1e5977ff5fca26449c53e00e4658b97
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

39 lines
1.4 KiB
Plaintext

import { SOCKET_IO_PING_INTERVAL, SOCKET_IO_PATH, SOCKET_IO_PING_CODE } from './constants';
import { QueryParams, SendMessage } from './types';
export const parseSocketIOUrl = (url: string) => {
if (url) {
const isSecure = /^https|wss/.test(url);
const strippedProtocol = url.replace(/^(https?|wss?)(:\/\/)?/, '');
const removedFinalBackSlack = strippedProtocol.replace(/\/$/, '');
const protocol = isSecure ? 'wss' : 'ws';
return `${protocol}://${removedFinalBackSlack}${SOCKET_IO_PATH}`;
} else if (url === '') {
const isSecure = /^https/.test(window.location.protocol);
const protocol = isSecure ? 'wss' : 'ws';
const port = window.location.port ? `:${window.location.port}` : '';
return `${protocol}://${window.location.hostname}${port}${SOCKET_IO_PATH}`;
}
return url;
};
export const appendQueryParams = (url: string, params: QueryParams = {}): string => {
const hasParamsRegex = /\?([\w]+=[\w]+)/;
const alreadyHasParams = hasParamsRegex.test(url);
const stringified = `${Object.entries(params).reduce((next, [key, value]) => {
return next + `${key}=${value}&`;
}, '').slice(0, -1)}`;
return `${url}${alreadyHasParams ? '&' : '?'}${stringified}`;
};
export const setUpSocketIOPing = (sendMessage: SendMessage, interval = SOCKET_IO_PING_INTERVAL) => {
const ping = () => sendMessage(SOCKET_IO_PING_CODE);
return window.setInterval(ping, interval);
};