본문 바로가기
버전: 10.x

WebSocket 링크

비공식 베타 번역

이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →

wsLink는 tRPC의 WebSockets 클라이언트 및 구독을 사용할 때 활용되는 종료 링크입니다. 자세한 내용은 여기에서 확인할 수 있습니다.

사용법

wsLink를 사용하려면 createWSClient로 생성할 수 있는 TRPCWebSocketClient를 전달해야 합니다:

client/index.ts
ts
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.ts
ts
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 함수에는 전달될 TRPCWebSocketClient가 필요하며, 이 클라이언트는 WebSocketClientOptions에 정의된 필드로 구성할 수 있습니다:

ts
export interface WebSocketLinkOptions {
client: TRPCWebSocketClient;
}
function createWSClient(opts: WebSocketClientOptions) => TRPCWebSocketClient
export 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) => TRPCWebSocketClient
export interface WebSocketClientOptions {
url: string;
WebSocket?: typeof WebSocket;
retryDelayMs?: typeof retryDelay;
onOpen?: () => void;
onClose?: (cause?: { code?: number }) => void;
}

참조

이 링크의 소스 코드는 GitHub에서 확인할 수 있습니다.