From 01114b2a911891151898ef93defd23af805480f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rik=20Levente?= Date: Sun, 17 Dec 2023 18:24:08 +0100 Subject: [PATCH] Add webrtc-client --- Cargo.lock | 20 +-- crates/client/Cargo.lock | 283 +++++++++++++++++++++++++++++++++++ crates/client/Cargo.toml | 15 ++ crates/client/src/lib.rs | 113 ++++++++++++++ interface/stores/settings.ts | 9 +- package.json | 1 + 6 files changed, 430 insertions(+), 11 deletions(-) create mode 100644 crates/client/Cargo.lock create mode 100644 crates/client/Cargo.toml create mode 100644 crates/client/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 7494ccc..1cf025d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "client" +version = "0.1.0" +dependencies = [ + "wasm-bindgen", + "wasm-peers", + "wasm-peers-protocol", + "web-sys", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -145,16 +155,6 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" -[[package]] -name = "p2p" -version = "0.1.0" -dependencies = [ - "wasm-bindgen", - "wasm-peers", - "wasm-peers-protocol", - "web-sys", -] - [[package]] name = "powershell_script" version = "1.1.0" diff --git a/crates/client/Cargo.lock b/crates/client/Cargo.lock new file mode 100644 index 0000000..0d1a4b3 --- /dev/null +++ b/crates/client/Cargo.lock @@ -0,0 +1,283 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bumpalo" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + +[[package]] +name = "js-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "p2p" +version = "0.1.0" +dependencies = [ + "wasm-bindgen", + "wasm-peers", + "wasm-peers-protocol", + "web-sys", +] + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" + +[[package]] +name = "serde" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde-json-wasm" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479b4dbc401ca13ee8ce902851b834893251404c4f3c65370a49e047a6be09a5" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "uuid" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +dependencies = [ + "getrandom", + "wasm-bindgen", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +dependencies = [ + "cfg-if", + "serde", + "serde_json", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac36a15a220124ac510204aec1c3e5db8a22ab06fd6706d881dc6149f8ed9a12" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" + +[[package]] +name = "wasm-peers" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "844021ded98404e3e28bd49b15968e4c56e84607a6126fa04cce492ba6f28518" +dependencies = [ + "console_error_panic_hook", + "js-sys", + "log", + "serde", + "serde-json-wasm", + "uuid", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-peers-protocol", + "web-sys", +] + +[[package]] +name = "wasm-peers-protocol" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a4960581ab8e7999bbbdc28f3807434de0e82a4d064846ffa1f5f0f2418e8" +dependencies = [ + "serde", +] + +[[package]] +name = "web-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +dependencies = [ + "js-sys", + "wasm-bindgen", +] diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml new file mode 100644 index 0000000..b619d8f --- /dev/null +++ b/crates/client/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "client" +version = "0.1.0" +edition = "2021" + +[lib] +name = "lib" +crate-type = ["cdylib", "rlib"] +path = "src/lib.rs" + +[dependencies] +wasm-bindgen = "0.2.89" +wasm-peers = "0.4.1" +wasm-peers-protocol = "0.3.2" +web-sys = "0.3.66" diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs new file mode 100644 index 0000000..c0e2de2 --- /dev/null +++ b/crates/client/src/lib.rs @@ -0,0 +1,113 @@ +use std::cell::RefCell; +use std::rc::Rc; +use wasm_bindgen::prelude::*; +use wasm_peers::one_to_many::{MiniClient, MiniServer}; +use wasm_peers::ConnectionType; +use wasm_peers_protocol::SessionId; +use web_sys::console; + +static SIGNALING_SERVER_URL: &str = "wss://slippery-chalk-production.up.railway.app/one-to-many"; +static STUN_SERVER_URL: &str = "stun:openrelay.metered.ca:80"; + +fn log(message: String) { + console::log_1(&message.into()); +} + +#[wasm_bindgen] +pub struct WebRtcHost { + server: MiniServer, + server_open_connections_count: Rc>, +} + +#[wasm_bindgen] +impl WebRtcHost { + #[wasm_bindgen(constructor)] + pub fn new() -> Result { + let mut server = MiniServer::new( + SIGNALING_SERVER_URL, + SessionId::new("dummy-session-id".to_string()), + ConnectionType::Stun { + urls: STUN_SERVER_URL.to_string(), + }, + )?; + let server_open_connections_count = Rc::new(RefCell::new(0)); + + let server_clone = server.clone(); + let server_on_open = { + let server_open_connections_count = server_open_connections_count.clone(); + move |user_id| { + log(format!("connection to user established: {:?}", user_id)); + *server_open_connections_count.borrow_mut() += 1; + if *server_open_connections_count.borrow() == 2 { + server_clone.send_message_to_all("ping!"); + } + } + }; + let server_on_message = { + move |user_id, message| { + log(format!( + "server received message from client {:?}: {}", + user_id, message + )); + } + }; + server.start(server_on_open, server_on_message); + + Ok(WebRtcHost { + server, + server_open_connections_count, + }) + } + + pub fn send_message_to_clients(&self, message: &str) { + self.server.send_message_to_all(message); + } +} + +#[wasm_bindgen] +pub struct WebRtcClient { + client: MiniClient, + message: Rc>, +} + +#[wasm_bindgen] +impl WebRtcClient { + #[wasm_bindgen(constructor)] + pub fn new() -> Result { + let data = Rc::new(RefCell::new(String::new())); + let data_clone = data.clone(); + + let mut client = MiniClient::new( + SIGNALING_SERVER_URL, + SessionId::new("dummy-session-id".to_string()), + ConnectionType::Stun { + urls: STUN_SERVER_URL.to_string(), + }, + )?; + + let client_on_open = || { + log(format!("client connected")); + }; + + let on_message_callback = move |message: String| { + // log(format!("client received message: {}", message)); + + *data.borrow_mut() = message; + }; + + client.start(client_on_open, on_message_callback); + + Ok(WebRtcClient { + client, + message: data_clone, + }) + } + + pub fn get_message(&self) -> String { + self.message.borrow().clone() + } + + pub fn send_message_to_host(&self, message: &str) { + self.client.clone().send_message_to_host(message).unwrap(); + } +} diff --git a/interface/stores/settings.ts b/interface/stores/settings.ts index 927dfa4..62bb27d 100644 --- a/interface/stores/settings.ts +++ b/interface/stores/settings.ts @@ -1,5 +1,4 @@ import { writable, get } from "svelte/store" -import build from "../../build.json" const defaultSettings: LibSettings = { interval: 2, @@ -9,11 +8,19 @@ const defaultSettings: LibSettings = { // Create store export const settings = writable(sessionStorage.settings ? JSON.parse(sessionStorage.settings) : defaultSettings) +settings.update((settings) => { + return { + ...settings, + mode: import.meta.env.VITE_CORES_MODE, + } +}) // Listen for store events settings.subscribe((data) => { console.log("Settings changed: ", data) + data.mode = import.meta.env.VITE_CORES_MODE + sessionStorage.setItem("settings", JSON.stringify(data)) }) diff --git a/package.json b/package.json index a68d5e3..e1b16b8 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dev:web": "node scripts/buildNumber.cjs alpha true && cross-env VITE_CORES_MODE=client vite interface --port 3001", "build:interface": "node scripts/buildNumber.cjs alpha false && cross-env VITE_CORES_MODE=app vite build interface", "build:library": "cargo build --manifest-path crates/library/Cargo.toml --release && node scripts/copyLib.js", + "build:wasm": "npx wasm-pack build crates/client --target web --release", "check": "svelte-check && tsc --noEmit" }, "devDependencies": {