WebSocket リンク
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
wsLink は 終端リンク であり、tRPCのWebSocketsクライアントとサブスクリプションを使用する際に利用されます。詳細はこちらでご覧いただけます。
使用方法
wsLink を使用するには、createWSClient で作成できる TRPCWebSocketClient を渡す必要があります:
client/index.tstsimport { 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.tstsimport { 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 関数には TRPCWebSocketClient の渡し込みが必要で、これは WebSocketClientOptions で定義されたフィールドで設定できます:
tsexport 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;}
tsexport 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;}
参考
このリンクのソースコードはGitHubで確認できます。