feat: add tss module demo

This commit is contained in:
2025-04-18 23:55:40 +08:00
parent fdc3985b93
commit 5e781499e9
12 changed files with 422 additions and 10 deletions

41
src/ws-adapter/index.ts Normal file
View File

@@ -0,0 +1,41 @@
const isBrowser = process?.env?.BROWSER === 'true';
import { EventEmitter } from 'events';
type WebSocketOptions = {
/**
* 是否拒绝不安全的证书, in node only
*/
rejectUnauthorized?: boolean;
headers?: Record<string, string>;
[key: string]: any;
};
export const initWs = async (url: string, options?: WebSocketOptions) => {
let ws: WebSocket;
if (isBrowser) {
ws = new WebSocket(url);
} else {
const WebSocket = await import('ws').then((module) => module.default);
const { rejectUnauthorized, headers, ...rest } = options || {};
ws = new WebSocket(url, {
rejectUnauthorized: rejectUnauthorized || true,
headers: headers,
...rest,
}) as any;
}
return ws;
};
interface EventEmitterOptions {
/**
* Enables automatic capturing of promise rejection.
*/
captureRejections?: boolean | undefined;
}
/**
* 初始化一个事件发射器
* @param opts 事件发射器选项
* @returns 事件发射器
*/
export const initEmitter = (opts?: EventEmitterOptions) => {
const emitter = new EventEmitter(opts);
return emitter;
};