Enlace WebSocket
Traducción Beta No Oficial
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
wsLink es un enlace terminal que se utiliza con el Cliente de WebSockets y Suscripciones de tRPC. Puedes obtener más información sobre esto aquí.
Uso
Para usar wsLink, debes pasarle un TRPCWebSocketClient, que puedes crear con createWSClient:
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 })],});
Opciones de wsLink
La función wsLink requiere que se le pase un TRPCWebSocketClient, el cual puede configurarse con los campos definidos en 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;}
Referencia
Puedes consultar el código fuente de este enlace en GitHub.