-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
962 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
50 changes: 50 additions & 0 deletions
50
wtx-instances/web-socket-examples/web-socket-concurrent-client.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//! WebSocket client that reads and writes frames in different tasks. | ||
extern crate tokio; | ||
extern crate wtx; | ||
extern crate wtx_instances; | ||
|
||
use tokio::{net::TcpStream, sync::Mutex}; | ||
use wtx::{ | ||
misc::{simple_seed, Arc, TokioRustlsConnector, Uri, Xorshift64}, | ||
web_socket::{Frame, OpCode, WebSocketBuffer, WebSocketClient}, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> wtx::Result<()> { | ||
let uri = Uri::new("ws://www.example.com"); | ||
let connector = TokioRustlsConnector::from_auto()?; | ||
let stream = TcpStream::connect(uri.hostname_with_implied_port()).await?; | ||
let ws = WebSocketClient::connect( | ||
(), | ||
[], | ||
false, | ||
Xorshift64::from(simple_seed()), | ||
connector.connect_without_client_auth(uri.hostname(), stream).await?, | ||
&uri.to_ref(), | ||
WebSocketBuffer::default(), | ||
|_| wtx::Result::Ok(()), | ||
) | ||
.await?; | ||
let (mut reader, mut writer) = ws.into_parts::<Arc<Mutex<_>>, _, _>(|el| tokio::io::split(el)); | ||
let reader_jh = tokio::spawn(async move { | ||
loop { | ||
let frame = reader.read_frame().await?; | ||
match (frame.op_code(), frame.text_payload()) { | ||
(_, Some(elem)) => println!("{elem}"), | ||
(OpCode::Close, _) => break, | ||
_ => {} | ||
} | ||
} | ||
wtx::Result::Ok(()) | ||
}); | ||
let writer_jh = tokio::spawn(async move { | ||
writer.write_frame(&mut Frame::new_fin(OpCode::Text, *b"Hi and Bye")).await?; | ||
writer.write_frame(&mut Frame::new_fin(OpCode::Close, [])).await?; | ||
wtx::Result::Ok(()) | ||
}); | ||
let (reader_rslt, writer_rslt) = tokio::join!(reader_jh, writer_jh); | ||
reader_rslt??; | ||
writer_rslt??; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.