WebSocket 链接
wsLink
是使用 tRPC 的 WebSockets 客户端和订阅时使用的 终止链接,你可以了解有关 此处 的更多信息。
¥wsLink
is a terminating link that's used when using tRPC's WebSockets Client and Subscriptions, which you can learn more about here.
用法
¥Usage
要使用 wsLink
,你需要向其传递 TRPCWebSocketClient
,你可以使用 createWSClient
创建它:
¥To use wsLink
, you need to pass it a TRPCWebSocketClient
, which you can create with createWSClient
:
client/index.tsts
import { createTRPCProxyClient, createWSClient, wsLink } from '@trpc/client';import type { AppRouter } from '../server';const wsClient = createWSClient({url: 'ws://localhost:3000',});const trpcClient = createTRPCProxyClient<AppRouter>({links: [wsLink<AppRouter>({ client: wsClient })],});
client/index.tsts
import { createTRPCProxyClient, createWSClient, wsLink } from '@trpc/client';import type { AppRouter } from '../server';const wsClient = createWSClient({url: 'ws://localhost:3000',});const trpcClient = createTRPCProxyClient<AppRouter>({links: [wsLink<AppRouter>({ client: wsClient })],});
wsLink
选项
¥wsLink
Options
wsLink
功能需要传递一个 TRPCWebSocketClient
,可以使用 WebSocketClientOptions
中定义的字段进行配置:
¥The wsLink
function requires a TRPCWebSocketClient
to be passed, which can be configured with the fields defined in WebSocketClientOptions
:
ts
export interface WebSocketLinkOptions {client: TRPCWebSocketClient;}function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClientexport interface WebSocketClientOptions {url: string;WebSocket?: typeof WebSocket;retryDelayMs?: typeof retryDelay;onOpen?: () => void;onClose?: (cause?: { code?: number }) => void;}
ts
export interface WebSocketLinkOptions {client: TRPCWebSocketClient;}function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClientexport interface WebSocketClientOptions {url: string;WebSocket?: typeof WebSocket;retryDelayMs?: typeof retryDelay;onOpen?: () => void;onClose?: (cause?: { code?: number }) => void;}
参考
¥Reference
你可以在 GitHub。 上查看此链接的源代码
¥You can check out the source code for this link on GitHub.