Skip to content

Commit 8dec808

Browse files
authored
Merge pull request #246 from jpoles1/master
Adding basic code comments to async-client example
2 parents b4f50dd + 469d211 commit 8dec808

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

examples/async-client.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,70 @@ use websocket::{ClientBuilder, OwnedMessage};
1313

1414
const CONNECTION: &'static str = "ws://127.0.0.1:2794";
1515

16+
// Async websocket chat client
1617
fn main() {
1718
println!("Connecting to {}", CONNECTION);
19+
20+
// Construct new Tokio runtime environment
1821
let mut runtime = tokio::runtime::current_thread::Builder::new()
1922
.build()
2023
.unwrap();
2124

22-
// standard in isn't supported in mio yet, so we use a thread
23-
// see https://github.com/carllerche/mio/issues/321
2425
let (usr_msg, stdin_ch) = mpsc::channel(0);
26+
27+
// Spawn new thread to read user input
28+
// stdin isn't supported in mio yet, so we use a thread
29+
// see https://github.com/carllerche/mio/issues/321
2530
thread::spawn(|| {
2631
let mut input = String::new();
2732
let mut stdin_sink = usr_msg.wait();
2833
loop {
34+
// Read user input from stdin
2935
input.clear();
3036
stdin().read_line(&mut input).unwrap();
37+
38+
// Trim whitespace and match input to known chat commands
39+
// If input is unknown, send trimmed input as a chat message
3140
let trimmed = input.trim();
32-
3341
let (close, msg) = match trimmed {
3442
"/close" => (true, OwnedMessage::Close(None)),
3543
"/ping" => (false, OwnedMessage::Ping(b"PING".to_vec())),
3644
_ => (false, OwnedMessage::Text(trimmed.to_string())),
3745
};
38-
46+
// Send message to websocket server
3947
stdin_sink
4048
.send(msg)
4149
.expect("Sending message across stdin channel.");
42-
50+
// If user entered the "/close" command, break the loop
4351
if close {
4452
break;
4553
}
4654
}
4755
});
4856

57+
// Construct a new connection to the websocket server
4958
let runner = ClientBuilder::new(CONNECTION)
5059
.unwrap()
5160
.add_protocol("rust-websocket")
5261
.async_connect_insecure()
5362
.and_then(|(duplex, _)| {
5463
let (sink, stream) = duplex.split();
5564
stream
65+
// Iterate over message as they arrive in stream
5666
.filter_map(|message| {
5767
println!("Received Message: {:?}", message);
68+
// Respond to close or ping commands from the server
5869
match message {
5970
OwnedMessage::Close(e) => Some(OwnedMessage::Close(e)),
6071
OwnedMessage::Ping(d) => Some(OwnedMessage::Pong(d)),
6172
_ => None,
6273
}
6374
})
75+
// Takes in messages from both sinks
6476
.select(stdin_ch.map_err(|_| WebSocketError::NoDataAvailable))
77+
// Return a future that completes once all incoming data from the above streams has been processed into the sink
6578
.forward(sink)
6679
});
80+
// Start our websocket client runner in the Tokio environment
6781
runtime.block_on(runner).unwrap();
6882
}

0 commit comments

Comments
 (0)