Skip to content

Commit

Permalink
refactoring and adding non-tls example
Browse files Browse the repository at this point in the history
  • Loading branch information
LimpidCrypto committed Aug 23, 2024
1 parent 955c773 commit f58e4bb
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 1,480 deletions.
14 changes: 12 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,23 @@ default = [
tcp = []
dns = ["embassy-net/dns"]
tls = []
websocket = []
websocket = ["embedded-websocket"]
json-rpc = []
std = [
"dep:tokio",
"tokio",
"embedded-io-adapters/tokio-1",
"embedded-websocket/std",
"tokio-rustls",
"webpki-roots",
]
webpki-roots = ["dep:webpki-roots"]

[[example]]
name = "std_async_web_socket_client"
path = "examples/std_async_web_socket_client.rs"
required-features = ["std", "tcp", "websocket"]

[[example]]
name = "std_tls_async_web_socket_client"
path = "examples/std_tls_async_web_socket_client.rs"
required-features = ["std", "tcp", "tls", "websocket"]
47 changes: 47 additions & 0 deletions examples/std_async_web_socket_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use em_as_net::{
client::websocket::{
AsyncWebSocketClient, ReadResult, WebSocketRead, WebSocketSendMessageType, WebSocketWrite,
},
core::tcp::TcpStream,
};
use rand::thread_rng;
use url::Url;

#[tokio::main]
async fn main() {
let uri = Url::parse("ws://ws.vi-server.org:80/mirror/").unwrap();
let mut stream = TcpStream::connect(&uri).await.unwrap();
println!("TCP Connected");
let mut buffer = [0u8; 4096];
let rng = thread_rng();
let mut websocket = AsyncWebSocketClient::open(&mut stream, &mut buffer, &uri, rng, None, None)
.await
.unwrap();
println!("WebSocket Connected");
websocket
.write(
&mut stream,
&mut buffer,
WebSocketSendMessageType::Text,
true,
"Hello World".as_bytes(),
)
.await
.unwrap();
println!("Message Sent");
loop {
let message = websocket
.try_read(&mut stream, &mut buffer)
.await
.unwrap()
.unwrap();
match message {
ReadResult::Text(text) => {
assert_eq!("Hello World".to_string(), text);
println!("Received message: {}", text);
}
_ => panic!("Expected 'Hello World' as text message."),
}
break;
}
}
File renamed without changes.
31 changes: 0 additions & 31 deletions src/core/framed/codec/decoder.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/core/framed/codec/encoder.rs

This file was deleted.

42 changes: 0 additions & 42 deletions src/core/framed/codec/mod.rs

This file was deleted.

74 changes: 0 additions & 74 deletions src/core/framed/errors.rs

This file was deleted.

Loading

0 comments on commit f58e4bb

Please sign in to comment.