Skip to content

Commit

Permalink
Add an echo server for testing (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Oct 1, 2023
1 parent 9e45965 commit baab726
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions echo_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "echo_server"
version = "0.1.0"
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
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"
3 changes: 3 additions & 0 deletions echo_server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A server you can use for testing - just echoes the input.

`cargo r -p echo_server`
22 changes: 22 additions & 0 deletions echo_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
});
}
}
1 change: 1 addition & 0 deletions example_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 10 additions & 1 deletion example_app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use eframe::egui;
use ewebsock::{WsEvent, WsMessage, WsReceiver, WsSender};

#[derive(Default)]
pub struct ExampleApp {
url: String,
error: String,
frontend: Option<FrontEnd>,
}

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"))]
Expand Down

0 comments on commit baab726

Please sign in to comment.