Lien WebSocket
Traduction Bêta Non Officielle
Cette page a été traduite par PageTurner AI (bêta). Non approuvée officiellement par le projet. Vous avez trouvé une erreur ? Signaler un problème →
wsLink est un lien terminal utilisé pour le client WebSockets et les abonnements de tRPC, dont vous pouvez découvrir davantage ici.
Utilisation
Pour utiliser wsLink, vous devez lui passer un TRPCWebSocketClient, que vous pouvez créer avec createWSClient :
client/index.tstsimport {createTRPCClient ,createWSClient ,wsLink } from '@trpc/client';import type {AppRouter } from './server';constwsClient =createWSClient ({url : 'ws://localhost:3000',});consttrpcClient =createTRPCClient <AppRouter >({links : [wsLink <AppRouter >({client :wsClient })],});
client/index.tstsimport {createTRPCClient ,createWSClient ,wsLink } from '@trpc/client';import type {AppRouter } from './server';constwsClient =createWSClient ({url : 'ws://localhost:3000',});consttrpcClient =createTRPCClient <AppRouter >({links : [wsLink <AppRouter >({client :wsClient })],});
Authentification / Paramètres de connexion
Options de wsLink / createWSClient
La fonction wsLink nécessite qu'un TRPCWebSocketClient lui soit passé, qui peut être configuré avec les champs définis dans WebSocketClientOptions :
tsexport interfaceWebSocketLinkOptions {client :TRPCWebSocketClient ;/*** Data transformer* @see https://trpc.io/docs/v11/data-transformers**/transformer ?:DataTransformerOptions ;}declare functioncreateWSClient (opts :WebSocketClientOptions ):TRPCWebSocketClient ;export interfaceWebSocketClientOptions {/*** The URL to connect to (can be a function that returns a URL)*/url : string | (() =>MaybePromise <string>);/*** Connection params that are available in `createContext()`* These are sent as the first message*/connectionParams ?:Record <string, string> | null | (() =>MaybePromise <Record <string, string> | null>);/*** Ponyfill which WebSocket implementation to use*/WebSocket ?: typeofWebSocket ;/*** The number of milliseconds before a reconnect is attempted.* @default {@link exponentialBackoff}*/retryDelayMs ?: typeofexponentialBackoff ;/*** Triggered when a WebSocket connection is established*/onOpen ?: () => void;/*** Triggered when a WebSocket connection encounters an error*/onError ?: (evt ?:Event ) => 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;};/*** Send ping messages to the server and kill the connection if no pong message is returned*/keepAlive ?: {/*** @default false*/enabled : boolean;/*** Send a ping message every this many milliseconds* @default 5_000*/intervalMs ?: number;/*** Close the WebSocket after this many milliseconds if the server does not respond* @default 1_000*/pongTimeoutMs ?: number;};/*** Custom encoder for wire encoding (e.g. custom binary formats)* @default jsonEncoder*/experimental_encoder ?:Encoder ;}
tsexport interfaceWebSocketLinkOptions {client :TRPCWebSocketClient ;/*** Data transformer* @see https://trpc.io/docs/v11/data-transformers**/transformer ?:DataTransformerOptions ;}declare functioncreateWSClient (opts :WebSocketClientOptions ):TRPCWebSocketClient ;export interfaceWebSocketClientOptions {/*** The URL to connect to (can be a function that returns a URL)*/url : string | (() =>MaybePromise <string>);/*** Connection params that are available in `createContext()`* These are sent as the first message*/connectionParams ?:Record <string, string> | null | (() =>MaybePromise <Record <string, string> | null>);/*** Ponyfill which WebSocket implementation to use*/WebSocket ?: typeofWebSocket ;/*** The number of milliseconds before a reconnect is attempted.* @default {@link exponentialBackoff}*/retryDelayMs ?: typeofexponentialBackoff ;/*** Triggered when a WebSocket connection is established*/onOpen ?: () => void;/*** Triggered when a WebSocket connection encounters an error*/onError ?: (evt ?:Event ) => 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;};/*** Send ping messages to the server and kill the connection if no pong message is returned*/keepAlive ?: {/*** @default false*/enabled : boolean;/*** Send a ping message every this many milliseconds* @default 5_000*/intervalMs ?: number;/*** Close the WebSocket after this many milliseconds if the server does not respond* @default 1_000*/pongTimeoutMs ?: number;};/*** Custom encoder for wire encoding (e.g. custom binary formats)* @default jsonEncoder*/experimental_encoder ?:Encoder ;}
Référence
Vous pouvez consulter le code source de ce lien sur GitHub.