Skip to content

Add embedded-io-async Framer #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/rust
{
"name": "Rust",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bullseye"

// Use 'mounts' to make the cargo cache persistent in a Docker Volume.
// "mounts": [
// {
// "source": "devcontainer-cargo-cache-${devcontainerId}",
// "target": "/usr/local/cargo",
// "type": "volume"
// }
// ]

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "rustc --version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ httparse = { version = "1.8.0", default-features = false }
rand_core = { version = "0.6.4", default-features = false }
base64 = { version = "0.13.1", default-features = false }
futures = { version = "0.3.28", default-features = false }
embedded-io-async = { version = "0.6.1", default-features = false, optional = true }

[dev-dependencies]
rand = "0.8.5"
bytes = "1.4.0"
tokio = { version = "1.28.2", features = ["macros", "rt-multi-thread"] }
tokio-util = { version = "0.7.8", features = ["net", "codec"] }
embedded-io-adapters = { version = "0.6.1", features = ["tokio-1"] }

# see readme for no_std support
[features]
default = ["std"]
# default = []
std = ["httparse/std"]
embedded-io-async = ["dep:embedded-io-async"]

[[example]]
name = "client_async_embedded_io_async"
path = "examples/client_async_embedded_io_async.rs"
required-features = ["embedded-io-async"]
71 changes: 71 additions & 0 deletions examples/client_async_embedded_io_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use embedded_io_adapters::tokio_1::FromTokio;
use std::error::Error;

use tokio::net::TcpStream;

use embedded_websocket::{
framer_async::{Framer, FramerError, ReadResult},
WebSocketClient, WebSocketCloseStatusCode, WebSocketOptions, WebSocketSendMessageType,
};

#[tokio::main]
async fn main() -> Result<(), FramerError<impl Error>> {
// Connect to a peer
let address = "127.0.0.1:1337";
let mut buffer = [0u8; 4000];
let tcp_stream = TcpStream::connect(address).await.map_err(FramerError::Io)?;
let mut stream = FromTokio::new(tcp_stream);
let websocket = WebSocketClient::new_client(rand::thread_rng());

// initiate a websocket opening handshake
let websocket_options = WebSocketOptions {
path: "/chat",
host: "localhost",
origin: "http://localhost:1337",
sub_protocols: None,
additional_headers: None,
};

let mut framer = Framer::new(websocket);

framer
.connect(&mut stream, &mut buffer, &websocket_options)
.await?;

println!("ws handshake complete");

framer
.write(
&mut stream,
&mut buffer,
WebSocketSendMessageType::Text,
true,
"Hello, world".as_bytes(),
)
.await?;

println!("sent message");

while let Some(read_result) = framer.read(&mut stream, &mut buffer).await {
let read_result = read_result?;
match read_result {
ReadResult::Text(text) => {
println!("received text: {text}");

framer
.close(
&mut stream,
&mut buffer,
WebSocketCloseStatusCode::NormalClosure,
None,
)
.await?
}
_ => { // ignore other kinds of messages
}
}
}

println!("closed");
Ok(())
}
Loading