fix: fix ws

This commit is contained in:
2025-06-23 10:38:01 +08:00
parent 203fa1f103
commit 767e436eb8
4 changed files with 24 additions and 19 deletions

View File

@@ -1,6 +1,13 @@
const isBrowser = process?.env?.BROWSER === 'true' || typeof window !== 'undefined' && typeof window.document !== 'undefined';
import { EventEmitter } from 'events';
const isBrowser = process?.env?.BROWSER === 'true' || (typeof window !== 'undefined' && typeof window.document !== 'undefined');
const chantHttpToWs = (url: string) => {
if (url.startsWith('http://')) {
return url.replace('http://', 'ws://');
}
if (url.startsWith('https://')) {
return url.replace('https://', 'wss://');
}
return url;
};
type WebSocketOptions = {
/**
* 是否拒绝不安全的证书, in node only
@@ -11,6 +18,7 @@ type WebSocketOptions = {
};
export const initWs = async (url: string, options?: WebSocketOptions) => {
let ws: WebSocket;
url = chantHttpToWs(url);
if (isBrowser) {
ws = new WebSocket(url);
} else {
@@ -30,12 +38,3 @@ interface EventEmitterOptions {
*/
captureRejections?: boolean | undefined;
}
/**
* 初始化一个事件发射器
* @param opts 事件发射器选项
* @returns 事件发射器
*/
export const initEmitter = (opts?: EventEmitterOptions) => {
const emitter = new EventEmitter(opts);
return emitter;
};