-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix features * remove tokio-tungstenite dependency from clients * remove tokio dependency from clients for WASM builds * add demo WASM client; bug fixes
- Loading branch information
Showing
21 changed files
with
423 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "ezsockets-chat-client-wasm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
async-trait = "0.1.52" | ||
console_error_panic_hook = "0.1" | ||
ezsockets = { path = "../../", default-features = false, features = ["wasm_client"] } | ||
tracing = "0.1.32" | ||
tracing-subscriber = "0.3.9" | ||
tracing-wasm = "0.2" | ||
url = "2.2.2" | ||
wasm-bindgen = "0.2" | ||
wasm-bindgen-futures = "0.4" | ||
wasmtimer = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1. install [wasm-server-runner](https://github.com/jakobhellermann/wasm-server-runner) | ||
2. `cd` to this directory | ||
3. `cargo run --target wasm32-unknown-unknown` | ||
4. paste the address provided into a new browser window | ||
5. open the browser window console to view the chat logs | ||
|
||
Note: This demo does not currently have a way to pass messages into the chat. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#![allow(unused_imports)] | ||
|
||
use async_trait::async_trait; | ||
use ezsockets::{ClientConfig, RawMessage, SocketConfig}; | ||
use std::io::BufRead; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
struct Client {} | ||
|
||
#[async_trait] | ||
impl ezsockets::ClientExt for Client { | ||
type Call = (); | ||
|
||
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> { | ||
tracing::info!("received message: {text}"); | ||
Ok(()) | ||
} | ||
|
||
async fn on_binary(&mut self, bytes: Vec<u8>) -> Result<(), ezsockets::Error> { | ||
tracing::info!("received bytes: {bytes:?}"); | ||
Ok(()) | ||
} | ||
|
||
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> { | ||
let () = call; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(target_family = "wasm")] | ||
#[wasm_bindgen::prelude::wasm_bindgen(main)] | ||
async fn main() -> Result<(), wasm_bindgen::JsValue> { | ||
// setup tracing | ||
console_error_panic_hook::set_once(); | ||
tracing_wasm::set_as_global_default(); | ||
|
||
// make client | ||
// - note: we use a hacky custom Ping/Pong protocol to keep the client alive (see the 'chat-server' example | ||
// for the Ping side via SessionExt::on_binary()) | ||
let config = ClientConfig::new("ws://localhost:8080/websocket").socket_config(SocketConfig { | ||
heartbeat_ping_msg_fn: Arc::new(|_t: Duration| RawMessage::Binary("ping".into())), | ||
..Default::default() | ||
}); | ||
let (_client, mut handle) = ezsockets::connect_with( | ||
|_client| Client {}, | ||
config, | ||
ezsockets::ClientConnectorWasm::default(), | ||
); | ||
|
||
// collect inputs: todo | ||
|
||
// keep main alive until it is manually terminated | ||
handle.extract().await.unwrap().unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
fn main() { | ||
// need per-package targets https://github.com/rust-lang/cargo/issues/9406 | ||
unreachable!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.