Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanja-4732 committed Aug 21, 2023
1 parent cce48cb commit 4b4b36b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ web-sys = "0.3.64"
wasm-bindgen-futures = "0.4.37"
gloo-net = { version = "0.3.1", features = ["http"] }
gloo-console = "0.2.3"
serde_json = "1.0.105"
30 changes: 26 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn _get_sounds_strings() -> String {
}

pub async fn get_sounds_strings() -> Result<String, gloo_net::Error> {
let req = RequestBuilder::new("http://licht.realraum.at:8080")
let req = RequestBuilder::new("http://licht.realraum.at:4242/sounds")
.method(Method::GET)
.mode(RequestMode::Cors)
.build()?;
Expand All @@ -32,7 +32,7 @@ pub async fn get_sounds_strings() -> Result<String, gloo_net::Error> {
resp.text().await
}

pub fn parse_sounds(txt: &str) -> Vec<Sound> {
pub fn _parse_sounds(txt: &str) -> Vec<Sound> {
let mut sounds = Vec::new();
for cap in SOUND_RX.captures_iter(txt) {
let name = cap[2].to_string();
Expand All @@ -42,6 +42,28 @@ pub fn parse_sounds(txt: &str) -> Vec<Sound> {
sounds
}

#[derive(Debug, Deserialize)]
struct ServerSound {
name: String,
path: String,
}

#[derive(Debug, Serialize)]
struct PlaySoundPayload {
name: String,
}

pub fn parse_sounds(txt: &str) -> Vec<Sound> {
let sounds: Vec<ServerSound> = serde_json::from_str(txt).unwrap();
sounds
.into_iter()
.map(|sound| Sound {
name: sound.name,
url: sound.path,
})
.collect()
}

pub async fn get_sounds() -> Result<Vec<Sound>, gloo_net::Error> {
let txt = get_sounds_strings().await?;
let mut sounds = parse_sounds(&txt);
Expand All @@ -50,12 +72,12 @@ pub async fn get_sounds() -> Result<Vec<Sound>, gloo_net::Error> {
}

pub async fn play_sound(url: String) -> Result<(), gloo_net::Error> {
let url = format!("http://licht.realraum.at:8080{}", url);
let url = format!("http://licht.realraum.at:4242/play/{}", url);
let req = RequestBuilder::new(&url)
.method(Method::GET)
.mode(RequestMode::Cors)
.cache(RequestCache::NoCache)
.build()?;
.body(Json(PlaySoundPayload { name: url.clone() }))?;

let resp = req.send().await?;

Expand Down

0 comments on commit 4b4b36b

Please sign in to comment.