Don't send stuff before client rendered it

This commit is contained in:
Gender Shrapnel 2020-06-01 18:58:26 +02:00
parent d7f606a036
commit 3ee735b7f2
2 changed files with 8 additions and 4 deletions

View File

@ -1,7 +1,7 @@
use std::sync::Arc;
use futures::SinkExt;
use futures::{SinkExt, StreamExt};
use serde_json::json;
use tera::{Context, Tera};
use tokio::sync::watch;
@ -53,12 +53,14 @@ struct WithTemplate {
context: Context,
}
async fn watch_state(mut ws: WebSocket, mut state_channel: watch::Receiver<State>) {
loop {
async fn watch_state(ws: WebSocket, mut state_channel: watch::Receiver<State>) {
let (mut tx, mut rx) = ws.split();
while let Some(_) = rx.next().await {
let state = state_channel.recv().await;
let result = match state {
Some(state) => ws.send(Message::text(json!(state).to_string())).await,
Some(state) => tx.send(Message::text(json!(state).to_string())).await,
None => break
};

View File

@ -19,6 +19,7 @@
ws.onopen = function() {
console.log("Connected");
ws.send("MOAR");
}
ws.onmessage = function(e) {
@ -33,6 +34,7 @@
li.innerText = `${ob.name} @ ${ob.x}, ${ob.y}`;
ul.append(li);
}
ws.send("MOAR");
};
</script>
</body>