| Crates.io | typed-websocket |
| lib.rs | typed-websocket |
| version | 0.1.0 |
| created_at | 2024-11-22 07:45:20.886104+00 |
| updated_at | 2024-11-22 07:45:20.886104+00 |
| description | simple wrapper on top of websocket stream that enforces type |
| homepage | |
| repository | https://github.com/kanekoshoyu/kucoin_arbitrage |
| max_upload_size | |
| id | 1457160 |
| size | 6,320 |
simple wrapper on top of websocket stream that enforces type
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
use tokio_tungstenite::connect_async;
use url::Url;
let url = Url::parse("wss://example.com/socket")?;
let (stream, _) = connect_async(url).await?;
// Create a TypedWebSocketStream with specific input/output types
let mut ws: TypedWebSocketStream<_, RequestMessage, ResponseMessage> = TypedWebSocketStream::new(stream);
// Send a message
let outgoing = RequestMessage::new();
ws.send(outgoing).await?;
// Receive a message
match ws.receive().await {
Ok(incoming) => println!("Received: {:?}", incoming),
Err(e) => eprintln!("Error receiving message: {}", e),
}
// Close the connection
ws.close().await?;
Ok(())
}
// Define input and output message types
#[derive(Serialize, Debug)]
struct RequestMessage {
command: String,
data: String,
}
#[derive(Deserialize, Debug)]
struct ResponseMessage {
response: String,
status: u16,
}