WebSocket 连接
wsLink
是一个 终止链接,用于使用 tRPC 的 WebSockets 客户端和订阅,您可以在 这里 了解更多信息。
使用
要使用 wsLink
,您需要传递一个 TRPCWebSocketClient
,您可以使用 createWSClient
创建它
client/index.tsts
import { createTRPCClient, createWSClient, wsLink } from '@trpc/client';import type { AppRouter } from '../server';const wsClient = createWSClient({url: 'ws://localhost:3000',});const trpcClient = createTRPCClient<AppRouter>({links: [wsLink<AppRouter>({ client: wsClient })],});
client/index.tsts
import { createTRPCClient, createWSClient, wsLink } from '@trpc/client';import type { AppRouter } from '../server';const wsClient = createWSClient({url: 'ws://localhost:3000',});const trpcClient = createTRPCClient<AppRouter>({links: [wsLink<AppRouter>({ client: wsClient })],});
wsLink
选项
wsLink
函数需要传递一个 TRPCWebSocketClient
,它可以使用 WebSocketClientOptions
中定义的字段进行配置
ts
export interface WebSocketLinkOptions {client: TRPCWebSocketClient;/*** Data transformer* @link https://trpc.node.org.cn/docs/v11/data-transformers**/transformer?: DataTransformerOptions;}function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClientexport interface WebSocketClientOptions {/*** The URL to connect to (can be a function that returns a URL)*/url: string | (() => MaybePromise<string>);/*** Ponyfill which WebSocket implementation to use*/WebSocket?: typeof WebSocket;/*** The number of milliseconds before a reconnect is attempted.* @default {@link exponentialBackoff}*/retryDelayMs?: typeof exponentialBackoff;/*** Triggered when a WebSocket connection is established*/onOpen?: () => void;/*** Triggered when a WebSocket connection is closed*/onClose?: (cause?: { code?: number }) => void;/*** Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)*/lazy?: {/*** Enable lazy mode* @default false*/enabled: boolean;/*** Close the WebSocket after this many milliseconds* @default 0*/closeMs: number;};}
ts
export interface WebSocketLinkOptions {client: TRPCWebSocketClient;/*** Data transformer* @link https://trpc.node.org.cn/docs/v11/data-transformers**/transformer?: DataTransformerOptions;}function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClientexport interface WebSocketClientOptions {/*** The URL to connect to (can be a function that returns a URL)*/url: string | (() => MaybePromise<string>);/*** Ponyfill which WebSocket implementation to use*/WebSocket?: typeof WebSocket;/*** The number of milliseconds before a reconnect is attempted.* @default {@link exponentialBackoff}*/retryDelayMs?: typeof exponentialBackoff;/*** Triggered when a WebSocket connection is established*/onOpen?: () => void;/*** Triggered when a WebSocket connection is closed*/onClose?: (cause?: { code?: number }) => void;/*** Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)*/lazy?: {/*** Enable lazy mode* @default false*/enabled: boolean;/*** Close the WebSocket after this many milliseconds* @default 0*/closeMs: number;};}
参考
您可以在 GitHub 上查看此链接的源代码。