generated from tailored/router-template
fix: fix ws
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user