WebSocket client for Cyaim.WebSocketServer with automatic endpoint discovery (JavaScript/TypeScript)
npm install @cyaim/websocket-clientimport { WebSocketClientFactory, endpoint } from '@cyaim/websocket-client';
// Define interface / 定义接口
interface IWeatherService {
getForecasts(): Promise<WeatherForecast[]>;
getForecast(city: string): Promise<WeatherForecast>;
}
// Create factory / 创建工厂
const factory = new WebSocketClientFactory('http://localhost:5000', '/ws');
// Create client / 创建客户端
const weatherService = await factory.createClient<IWeatherService>({
getForecasts: async () => {},
getForecast: async (city: string) => {}
});
// Use client / 使用客户端
const forecasts = await weatherService.getForecasts();
const forecast = await weatherService.getForecast('Beijing');import { endpoint } from '@cyaim/websocket-client';
interface IUserService {
getUserById(id: number): Promise<User>;
}
const userService = await factory.createClient<IUserService>({
[endpoint('user.getbyid')]: async (id: number) => {}
});import { WebSocketClientOptions } from '@cyaim/websocket-client';
const options = new WebSocketClientOptions();
options.lazyLoadEndpoints = true; // Load endpoints on demand
options.validateAllMethods = false;
const factory = new WebSocketClientFactory('http://localhost:5000', '/ws', options);new WebSocketClientFactory(serverBaseUrl: string, channel?: string, options?: WebSocketClientOptions)getEndpoints(): Promise<WebSocketEndpointInfo[]>- Get all endpoints from servercreateClient<T>(interfaceDefinition: T): Promise<T>- Create client proxy
validateAllMethods: boolean- Validate all methods have endpoints (default: false)lazyLoadEndpoints: boolean- Load endpoints on demand (default: false)throwOnEndpointNotFound: boolean- Throw error if endpoint not found (default: true)
MIT