Skip to main content
Version: 11.x

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.ts
ts
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.ts
ts
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 })],
});

身份验证/连接参数

¥Authentication / Connection params

在此处查看更多

¥See more here

¥wsLink / createWSClient 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;
/**
* Data transformer
* @see https://trpc.nodejs.cn/docs/v11/data-transformers
**/
transformer?: DataTransformerOptions;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export interface WebSocketClientOptions {
/**
* 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: 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 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;
};
}
ts
export interface WebSocketLinkOptions {
client: TRPCWebSocketClient;
/**
* Data transformer
* @see https://trpc.nodejs.cn/docs/v11/data-transformers
**/
transformer?: DataTransformerOptions;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export interface WebSocketClientOptions {
/**
* 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: 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 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;
};
}

参考

¥Reference

你可以在 GitHub。 上查看此链接的源代码

¥You can check out the source code for this link on GitHub.