Crate web_socket
source ·Expand description
Web-Socket
This library provide WebSocket implementation for both client and server.
Usage
Run:
cargo add web-socket
Or add this to your Cargo.toml file.
[dependencies]
web-socket = "0.3"
Ping-Pong Example
You can run this example with: cargo run --example ping_pong
use std::io::Result;
use web_socket::{client::WSS, DataType, Event};
async fn example() -> Result<()> {
let mut ws = WSS::connect("ws.ifelse.io:443", "/").await?;
ws.on_event = Box::new(|ev| {
if let Event::Pong(_) = ev {
println!("Pong: {}", ev.to_string());
}
Ok(())
});
for _ in 0..5 {
ws.send(Event::Ping(b"Hello!")).await?;
ws.send("Copy Cat!").await?;
let mut data = ws.recv().await?;
assert_eq!(data.ty, DataType::Text);
let mut buf = vec![];
data.read_to_end(&mut buf).await?;
println!("Text: {:?}", String::from_utf8(buf));
}
Ok(())
}For more examples, see ./examples directory.
It passed every test of the autobahn testsuite
License
This project is licensed under Apache License 2.0
Modules
- client specific implementation
- Client handshake request
- This module contain some utility function to work with http protocol.
- server specific implementation
Structs
- WebSocket implementation for both client and server
Enums
- When closing an established connection an endpoint MAY indicate a reason for closure.
- The CloseEvent enum represents the possible events that can occur when a WebSocket connection is closed. It has two variants:
ErrorandClose. - It represent the type of data that is being sent over the WebSocket connection.
- Represent a websocket event, either
PingorPong
Constants
- Used to represent
WebSocket<CLIENT>type. - Used to represent
WebSocket<SERVER>type.
Traits
- This trait is responsible for encoding websocket closed frame.
- This trait is responsible for encoding websocket messages.