Skip to content

Commit

Permalink
fix: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
PrivateRookie committed Nov 6, 2023
1 parent ac8fcd5 commit d6c0870
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 308 deletions.
32 changes: 12 additions & 20 deletions examples/autobahn_async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,29 @@ use ws_tool::{
codec::{AsyncFrameCodec, AsyncStringCodec},
errors::WsError,
frame::OpCode,
ClientBuilder,
ClientConfig,
};

const AGENT: &str = "async-client";

async fn get_case_count() -> Result<usize, WsError> {
let mut client = ClientBuilder::new()
.async_connect(
"ws://localhost:9002/getCaseCount".parse().unwrap(),
let mut client = ClientConfig::default()
.async_connect_with(
"ws://localhost:9002/getCaseCount",
AsyncStringCodec::check_fn,
)
.await
.unwrap();
let msg = client.receive().await.unwrap().data.parse().unwrap();
client.receive().await.unwrap();
// send_close(&mut client, 1001, "".to_string()).unwrap();
let msg = client.receive().await.unwrap();
let msg = msg.data.parse().unwrap();
Ok(msg)
}

async fn run_test(case: usize) -> Result<(), WsError> {
info!("running test case {}", case);
let url: http::Uri = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT)
.parse()
.unwrap();
let (mut read, mut write) = ClientBuilder::new()
.async_connect(url, AsyncFrameCodec::check_fn)
let url = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT);
let (mut read, mut write) = ClientConfig::default()
.async_connect_with(url, AsyncFrameCodec::check_fn)
.await
.unwrap()
.split();
Expand Down Expand Up @@ -79,14 +76,9 @@ async fn run_test(case: usize) -> Result<(), WsError> {
}

async fn update_report() -> Result<(), WsError> {
let url: http::Uri = format!("ws://localhost:9002/updateReports?agent={}", AGENT)
.parse()
.unwrap();
let mut client = ClientBuilder::new()
.async_connect(url, AsyncStringCodec::check_fn)
.await
.unwrap();
client.send((1000u16, String::new())).await.map(|_| ())
let url = format!("ws://localhost:9002/updateReports?agent={}", AGENT);
let mut client = ClientConfig::default().async_connect(url).await.unwrap();
client.close(1000u16, &[]).await
}

#[tokio::main]
Expand Down
54 changes: 20 additions & 34 deletions examples/autobahn_async_deflate_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ use rand::random;
use tracing::*;
use tracing_subscriber::util::SubscriberInitExt;
use ws_tool::{
codec::{AsyncDeflateCodec, AsyncStringCodec, PMDConfig, WindowBit},
codec::{AsyncStringCodec, WindowBit},
errors::WsError,
frame::{OpCode, OwnedFrame},
ClientBuilder,
ClientConfig,
};

const AGENT: &str = "async-deflate-client";

async fn get_case_count() -> Result<usize, WsError> {
let mut client = ClientBuilder::new()
.async_connect(
"ws://localhost:9002/getCaseCount".parse().unwrap(),
let mut client = ClientConfig::default()
.async_connect_with(
"ws://localhost:9002/getCaseCount",
AsyncStringCodec::check_fn,
)
.await
.unwrap();
let msg = client.receive().await.unwrap().data.parse().unwrap();
client.receive().await.unwrap();
let msg = client.receive().await.unwrap();
let msg = msg.data.parse().unwrap();
Ok(msg)
}

Expand All @@ -30,25 +30,16 @@ fn mask_key() -> [u8; 4] {

async fn run_test(case: usize) -> Result<(), WsError> {
info!("running test case {}", case);
let url: http::Uri = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT)
.parse()
.unwrap();
let config = PMDConfig {
server_max_window_bits: WindowBit::Nine,
client_max_window_bits: WindowBit::Nine,
..PMDConfig::default()
};
let (mut read, mut write) = match ClientBuilder::new()
.extension(config.ext_string())
.async_connect(url, AsyncDeflateCodec::check_fn)
.await
{
Ok(client) => client.split(),
Err(e) => {
tracing::error!("{e}");
return Ok(());
}
};
let url = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT);
let (mut read, mut write) = ClientConfig {
window: Some(WindowBit::Nine),
..Default::default()
}
.async_connect(url)
.await
.unwrap()
.split();

let now = std::time::Instant::now();
loop {
match read.receive().await {
Expand Down Expand Up @@ -100,14 +91,9 @@ async fn run_test(case: usize) -> Result<(), WsError> {
}

async fn update_report() -> Result<(), WsError> {
let url: http::Uri = format!("ws://localhost:9002/updateReports?agent={}", AGENT)
.parse()
.unwrap();
let mut client = ClientBuilder::new()
.async_connect(url, AsyncStringCodec::check_fn)
.await
.unwrap();
client.send((1000u16, String::new())).await.map(|_| ())
let url = format!("ws://localhost:9002/updateReports?agent={}", AGENT);
let mut client = ClientConfig::default().async_connect(url).await.unwrap();
client.close(1000u16, &[]).await
}

#[tokio::main]
Expand Down
25 changes: 9 additions & 16 deletions examples/autobahn_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@ use ws_tool::{
codec::{FrameCodec, StringCodec},
errors::WsError,
frame::OpCode,
ClientBuilder,
ClientConfig,
};

const AGENT: &str = "client";

fn get_case_count() -> Result<usize, WsError> {
let uri = "ws://localhost:9002/getCaseCount";
let mut client = ClientBuilder::new()
.connect(uri.parse().unwrap(), StringCodec::check_fn)
let mut client = ClientConfig::default()
.connect_with(uri, StringCodec::check_fn)
.unwrap();
let msg = client.receive().unwrap().data.parse().unwrap();
client.receive().unwrap();
Ok(msg)
}

fn run_test(case: usize) -> Result<(), WsError> {
info!("running test case {}", case);
let url: http::Uri = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT)
.parse()
.unwrap();
let (mut read, mut write) = ClientBuilder::new()
.connect(url, FrameCodec::check_fn)
let url = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT);
let (mut read, mut write) = ClientConfig::default()
.connect_with(url, FrameCodec::check_fn)
.unwrap()
.split();
loop {
Expand Down Expand Up @@ -76,13 +73,9 @@ fn run_test(case: usize) -> Result<(), WsError> {
}

fn update_report() -> Result<(), WsError> {
let url: http::Uri = format!("ws://localhost:9002/updateReports?agent={}", AGENT)
.parse()
.unwrap();
let mut client = ClientBuilder::new()
.connect(url, StringCodec::check_fn)
.unwrap();
client.send((1000u16, String::new())).map(|_| ())
let url = format!("ws://localhost:9002/updateReports?agent={}", AGENT);
let mut client = ClientConfig::default().connect(url).unwrap();
client.close(1000u16, &[]).map(|_| ())
}

fn main() -> Result<(), ()> {
Expand Down
49 changes: 16 additions & 33 deletions examples/autobahn_deflate_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ use rand::random;
use tracing::*;
use tracing_subscriber::util::SubscriberInitExt;
use ws_tool::{
codec::{DeflateCodec, PMDConfig, StringCodec, WindowBit},
codec::{StringCodec, WindowBit},
errors::WsError,
frame::{OpCode, OwnedFrame},
ClientBuilder,
ClientConfig,
};

const AGENT: &str = "deflate-client";

fn get_case_count() -> Result<usize, WsError> {
let mut client = ClientBuilder::new()
.connect(
"ws://localhost:9002/getCaseCount".parse().unwrap(),
StringCodec::check_fn,
)
let uri = "ws://localhost:9002/getCaseCount";
let mut client = ClientConfig::default()
.connect_with(uri, StringCodec::check_fn)
.unwrap();
let msg = client.receive().unwrap().data.parse().unwrap();
client.receive().unwrap();
Ok(msg)
}

Expand All @@ -28,24 +25,14 @@ fn mask_key() -> [u8; 4] {

fn run_test(case: usize) -> Result<(), WsError> {
info!("running test case {}", case);
let url: http::Uri = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT)
.parse()
.unwrap();
let config = PMDConfig {
server_max_window_bits: WindowBit::Nine,
client_max_window_bits: WindowBit::Nine,
..PMDConfig::default()
};
let (mut read, mut write) = match ClientBuilder::new()
.extension(config.ext_string())
.connect(url, DeflateCodec::check_fn)
{
Ok(client) => client.split(),
Err(e) => {
tracing::error!("{e}");
return Ok(());
}
};
let url = format!("ws://localhost:9002/runCase?case={}&agent={}", case, AGENT);
let (mut read, mut write) = ClientConfig {
window: Some(WindowBit::Nine),
..Default::default()
}
.connect(url)
.unwrap()
.split();
let now = std::time::Instant::now();
loop {
match read.receive() {
Expand Down Expand Up @@ -95,13 +82,9 @@ fn run_test(case: usize) -> Result<(), WsError> {
}

fn update_report() -> Result<(), WsError> {
let url: http::Uri = format!("ws://localhost:9002/updateReports?agent={}", AGENT)
.parse()
.unwrap();
let mut client = ClientBuilder::new()
.connect(url, StringCodec::check_fn)
.unwrap();
client.send((1000u16, String::new())).map(|_| ())
let url = format!("ws://localhost:9002/updateReports?agent={}", AGENT);
let mut client = ClientConfig::default().connect(url).unwrap();
client.close(1000u16, &[]).map(|_| ())
}

fn main() -> Result<(), ()> {
Expand Down
68 changes: 0 additions & 68 deletions examples/deflate_client.rs

This file was deleted.

51 changes: 0 additions & 51 deletions examples/deflate_server.rs

This file was deleted.

Loading

0 comments on commit d6c0870

Please sign in to comment.