WIP
This commit is contained in:
1309
Cargo.lock
generated
1309
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -6,3 +6,5 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tokio = "*"
|
||||
websockets = "0.3.0"
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use websockets::{WebSocket, WebSocketError};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), WebSocketError> {
|
||||
let mut ws = WebSocket::connect("ws://localhost:8080/").await?;
|
||||
|
||||
ws.send_text("foo".to_string()).await?;
|
||||
// ws.receive().await?;
|
||||
// ws.close(None).await?;
|
||||
loop {
|
||||
let msg = ws.receive().await?;
|
||||
println!("Received: {:?}", msg);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,3 +6,4 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
simple-websockets = "0.1.6"
|
||||
|
||||
@@ -1,3 +1,33 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use simple_websockets::{Event, Responder};
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let event_hub = simple_websockets::launch(8080).expect("failed to listen on port 8080");
|
||||
let mut clients: HashMap<u64, Responder> = HashMap::new();
|
||||
println!("Listening for websocket connection...");
|
||||
loop {
|
||||
match event_hub.poll_event() {
|
||||
Event::Connect(client_id, responder) => {
|
||||
println!("A client connected with id #{}", client_id);
|
||||
// add their Responder to our `clients` map:
|
||||
clients.insert(client_id, responder);
|
||||
}
|
||||
Event::Disconnect(client_id) => {
|
||||
println!("Client #{} disconnected.", client_id);
|
||||
// remove the disconnected client from the clients map:
|
||||
clients.remove(&client_id);
|
||||
}
|
||||
Event::Message(client_id, message) => {
|
||||
println!(
|
||||
"Received a message from client #{}: {:?}",
|
||||
client_id, message
|
||||
);
|
||||
// retrieve this client's `Responder`:
|
||||
let responder = clients.get(&client_id).unwrap();
|
||||
// echo the message back:
|
||||
responder.send(format!("Echo {}", message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user