WebSocket client for Cyaim.WebSocketServer with automatic endpoint discovery (Rust)
Add to your Cargo.toml:
[dependencies]
cyaim-websocket-client = { path = "../cyaim-websocket-client-rs" }
# Or from git:
# cyaim-websocket-client = { git = "https://github.com/cyaim/websocket-server" }use cyaim_websocket_client::{WebSocketClientFactory, WebSocketClientOptions};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct WeatherForecast {
date: String,
temperature_c: i32,
summary: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut factory = WebSocketClientFactory::new(
"http://localhost:5000".to_string(),
"/ws".to_string(),
None,
);
let client = factory.create_client();
// Get forecasts
let forecasts: Vec<WeatherForecast> = client
.send_request("weatherforecast.get", None::<()>)
.await?;
// Get forecast by city
let request_body = serde_json::json!({ "city": "Beijing" });
let forecast: WeatherForecast = client
.send_request("weatherforecast.getbycity", Some(request_body))
.await?;
Ok(())
}use cyaim_websocket_client::WebSocketClientOptions;
let options = WebSocketClientOptions {
lazy_load_endpoints: true,
validate_all_methods: false,
throw_on_endpoint_not_found: true,
};
let mut factory = WebSocketClientFactory::new(
"http://localhost:5000".to_string(),
"/ws".to_string(),
Some(options),
);new(server_base_url: String, channel: String, options: Option<WebSocketClientOptions>)- Create factoryget_endpoints() -> Result<Vec<WebSocketEndpointInfo>>- Get all endpointscreate_client() -> WebSocketClient- Create clientfind_endpoint(method_name: &str, target: Option<&str>) -> Result<Option<WebSocketEndpointInfo>>- Find endpoint
send_request<TRequest, TResponse>(target: &str, request_body: Option<TRequest>) -> Result<TResponse>- Send request
MIT