Skip to content
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

Use trunk for web demo #38

Merged
merged 10 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ This is a simple [WebSocket](https://en.wikipedia.org/wiki/WebSocket) library fo
## Usage

``` rust
let (mut sender, receiver) = ewebsock::connect("ws://example.com").unwrap();
let options = ewebsock::Options::default();
// see documentation for more options
let (mut sender, receiver) = ewebsock::connect("ws://example.com", options).unwrap();
sender.send(ewebsock::WsMessage::Text("Hello!".into()));
while let Some(event) = receiver.try_recv() {
println!("Received {:?}", event);
}
```

## Testing

First start the example echo server with:

```sh
cargo r -p echo_server
```

Then test the native library with:
Then test the library with:

```sh
# native mode
cargo run -p example_app
```

And the web library with:
```sh
./example_app/start_server.sh &
./example_app/build_web.sh --open
# web mode
cd example_app/ && trunk serve
Its-Just-Nans marked this conversation as resolved.
Show resolved Hide resolved
```
1 change: 1 addition & 0 deletions example_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
6 changes: 1 addition & 5 deletions example_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"]
publish = false


[lib]
crate-type = ["cdylib", "rlib"]


[features]
default = []

Expand All @@ -25,10 +21,10 @@ ewebsock = { path = "../ewebsock", features = ["tls"] }

eframe = "0.26.2" # Gives us egui, epi and web+native backends
log = "0.4"
env_logger = "0.10"

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger = "0.10"
tokio = { version = "1.16", optional = true, features = [
"macros",
"rt-multi-thread",
Expand Down
75 changes: 0 additions & 75 deletions example_app/build_web.sh

This file was deleted.

114 changes: 114 additions & 0 deletions example_app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<!DOCTYPE html>
Its-Just-Nans marked this conversation as resolved.
Show resolved Hide resolved
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- Disable zooming: -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />

<head>
<!-- change this to your project name -->
<title>ewebsock demo</title>

<!-- config for our rust wasm binary. go to https://trunkrs.dev/assets/#rust for more customization -->
<link data-trunk rel="rust" data-wasm-opt="2" />
<!-- this is the base url relative to which other urls will be constructed. trunk will insert this from the public-url option -->
<base data-trunk-public-url />

<meta name="theme-color" media="(prefers-color-scheme: light)" content="white" />
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#404040" />

<style>
html {
/* Remove touch delay: */
touch-action: manipulation;
}

body {
/* Light mode background color for what is not covered by the egui canvas,
or where the egui canvas is translucent. */
background: #909090;
}

@media (prefers-color-scheme: dark) {
body {
/* Dark mode background color for what is not covered by the egui canvas,
or where the egui canvas is translucent. */
background: #404040;
}
}

/* Allow canvas to fill entire web page: */
html,
body {
overflow: hidden;
margin: 0 !important;
padding: 0 !important;
height: 100%;
width: 100%;
}

/* Position canvas in center-top: */
canvas {
margin-right: auto;
margin-left: auto;
display: block;
position: absolute;
top: 0%;
left: 50%;
transform: translate(-50%, 0%);
}

.centered {
margin-right: auto;
margin-left: auto;
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #f0f0f0;
font-size: 24px;
font-family: Ubuntu-Light, Helvetica, sans-serif;
text-align: center;
}

/* ---------------------------------------------- */
/* Loading animation from https://loading.io/css/ */
.lds-dual-ring {
display: inline-block;
width: 24px;
height: 24px;
}

.lds-dual-ring:after {
content: " ";
display: block;
width: 24px;
height: 24px;
margin: 0px;
border-radius: 50%;
border: 3px solid #fff;
border-color: #fff transparent #fff transparent;
animation: lds-dual-ring 1.2s linear infinite;
}

@keyframes lds-dual-ring {
0% {
transform: rotate(0deg);
}

100% {
transform: rotate(360deg);
}
}
</style>
</head>

<body>
<!-- The WASM code will resize the canvas dynamically -->
<!-- the id is hardcoded in main.rs . so, make sure both match. -->
<canvas id="the_canvas_id"></canvas>
</body>
</html>

<!-- Powered by egui: https://github.com/emilk/egui/ -->
12 changes: 0 additions & 12 deletions example_app/setup_web.sh

This file was deleted.

23 changes: 23 additions & 0 deletions example_app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ fn main() -> eframe::Result<()> {
main_impl()
}

// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
fn main_impl() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).

Expand All @@ -24,3 +26,24 @@ fn main_impl() -> Result<(), eframe::Error> {
Box::new(|_cc| Box::new(app)),
)
}

// When compiling to web using trunk:
#[cfg(target_arch = "wasm32")]
fn main_impl() -> Result<(), eframe::Error> {
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();

let web_options = eframe::WebOptions::default();

wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new()
.start(
"the_canvas_id", // hardcode it
web_options,
Box::new(|_cc| Box::new(example_app::ExampleApp::default())),
)
.await
.expect("failed to start eframe");
});
Ok(())
}
14 changes: 0 additions & 14 deletions example_app/start_server.sh

This file was deleted.

Loading