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

View File

@@ -1,5 +1,5 @@
// import WebSocket from 'ws';
import { initWs } from '../../ws-adapter/index.ts';
import { initWs } from '../../../ws-adapter/index.ts';
export type VideoWSOptions = {
url?: string;

View File

@@ -1,4 +1,4 @@
import { initWs } from '../../ws-adapter/index.ts';
import { initWs } from '../../../ws-adapter/index.ts';
import { WSServer } from '../../provider/ws-server.ts';
import { nanoid } from 'nanoid';

View File

@@ -1,5 +1,5 @@
import { EventEmitter } from 'eventemitter3';
import { initWs } from '../ws-adapter/index.ts';
import { initWs } from '../../ws-adapter/index.ts';
import type { ClientOptions } from 'ws';
type WSSOptions = {
url: string;

View File

@@ -1,18 +0,0 @@
// @ts-nocheck
// https://github.com/maxogden/websocket-stream/blob/48dc3ddf943e5ada668c31ccd94e9186f02fafbd/ws-fallback.js
let ws: typeof WebSocket;
if (typeof WebSocket !== 'undefined') {
ws = WebSocket;
} else if (typeof MozWebSocket !== 'undefined') {
ws = MozWebSocket;
} else if (typeof global !== 'undefined') {
ws = global.WebSocket || global.MozWebSocket;
} else if (typeof window !== 'undefined') {
ws = window.WebSocket || window.MozWebSocket;
} else if (typeof self !== 'undefined') {
ws = self.WebSocket || self.MozWebSocket;
}
export default ws;

View File

@@ -1,41 +0,0 @@
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;
};

View File

@@ -1,3 +0,0 @@
import ws from 'ws';
export default ws;