From baab726ce2d96f46440bb145cb75a6c88b2d59b0 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 1 Oct 2023 11:01:21 +0200 Subject: [PATCH] Add an echo server for testing (#21) --- Cargo.lock | 7 +++++++ Cargo.toml | 2 +- README.md | 9 +++++++-- echo_server/Cargo.toml | 13 +++++++++++++ echo_server/README.md | 3 +++ echo_server/src/main.rs | 22 ++++++++++++++++++++++ example_app/Cargo.toml | 1 + example_app/src/app.rs | 11 ++++++++++- 8 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 echo_server/Cargo.toml create mode 100644 echo_server/README.md create mode 100644 echo_server/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 526adc2..61a7e47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -721,6 +721,13 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "echo_server" +version = "0.1.0" +dependencies = [ + "tungstenite", +] + [[package]] name = "ecolor" version = "0.22.0" diff --git a/Cargo.toml b/Cargo.toml index 644c184..a730cd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["ewebsock", "example_app"] +members = ["ewebsock", "example_app", "echo_server"] [patch.crates-io] # If you want to use the bleeding edge version of egui/eframe: diff --git a/README.md b/README.md index 8e570f6..d71a418 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,18 @@ while let Some(event) = receiver.try_recv() { ``` ## Testing -Locally: +First start the example echo server with: +```sh +cargo r -p echo_server +``` + +Then test the native library with: ```sh cargo run -p example_app ``` -Web: +And the web library with: ```sh ./example_app/start_server.sh & ./example_app/build_web.sh --open diff --git a/echo_server/Cargo.toml b/echo_server/Cargo.toml new file mode 100644 index 0000000..1da6dbf --- /dev/null +++ b/echo_server/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "echo_server" +version = "0.1.0" +authors = ["Emil Ernerfeldt "] +edition = "2021" +rust-version = "1.56" +license = "MIT OR Apache-2.0" +include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"] +publish = false + + +[dependencies] +tungstenite = "0.20" diff --git a/echo_server/README.md b/echo_server/README.md new file mode 100644 index 0000000..39c0e16 --- /dev/null +++ b/echo_server/README.md @@ -0,0 +1,3 @@ +A server you can use for testing - just echoes the input. + +`cargo r -p echo_server` diff --git a/echo_server/src/main.rs b/echo_server/src/main.rs new file mode 100644 index 0000000..50b4b9a --- /dev/null +++ b/echo_server/src/main.rs @@ -0,0 +1,22 @@ +use std::{net::TcpListener, thread::spawn}; + +fn main() { + let bind_addr = "127.0.0.1:9001"; + let server = TcpListener::bind(bind_addr).unwrap(); + eprintln!("Listening on: ws://{bind_addr}"); + for stream in server.incoming() { + spawn(move || { + let mut websocket = tungstenite::accept(stream.unwrap()).unwrap(); + eprintln!("New client connected"); + loop { + let msg = websocket.read().unwrap(); + + // We do not want to send back ping/pong messages. + if msg.is_binary() || msg.is_text() { + websocket.send(msg).unwrap(); + eprintln!("Responded."); + } + } + }); + } +} diff --git a/example_app/Cargo.toml b/example_app/Cargo.toml index 47d14d3..311cb92 100644 --- a/example_app/Cargo.toml +++ b/example_app/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" rust-version = "1.56" license = "MIT OR Apache-2.0" include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"] +publish = false [lib] diff --git a/example_app/src/app.rs b/example_app/src/app.rs index 8a09d2c..2e56f0c 100644 --- a/example_app/src/app.rs +++ b/example_app/src/app.rs @@ -1,13 +1,22 @@ use eframe::egui; use ewebsock::{WsEvent, WsMessage, WsReceiver, WsSender}; -#[derive(Default)] pub struct ExampleApp { url: String, error: String, frontend: Option, } +impl Default for ExampleApp { + fn default() -> Self { + Self { + url: "ws://127.0.0.1:9001".to_owned(), + error: Default::default(), + frontend: None, + } + } +} + impl eframe::App for ExampleApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { #[cfg(not(target_arch = "wasm32"))]