heartbeat
This commit is contained in:
@@ -2,5 +2,5 @@ use frontend::event_bus::EventBus;
|
||||
use yew_agent::PublicWorker;
|
||||
|
||||
fn main() {
|
||||
EventBus::<common::WebSocketMessage>::register();
|
||||
EventBus::<common::State>::register();
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ struct AppStateInner {
|
||||
state: common::State,
|
||||
}
|
||||
impl Reducible for AppStateInner {
|
||||
type Action = common::WebSocketMessage;
|
||||
type Action = common::State;
|
||||
fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> {
|
||||
Rc::new(Self { state: action })
|
||||
}
|
||||
@@ -63,7 +63,7 @@ pub fn App() -> Html {
|
||||
let _event_bus = use_memo(
|
||||
|_| {
|
||||
log::info!("Creating event bus bridge.");
|
||||
EventBus::<common::WebSocketMessage>::bridge(Rc::new(move |ws_msg: common::WebSocketMessage| {
|
||||
EventBus::<common::State>::bridge(Rc::new(move |ws_msg: common::State| {
|
||||
log::debug!("dispatching websocket msg to reducer");
|
||||
app_state_dispatcher.dispatch(ws_msg);
|
||||
}))
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
use crate::event_bus::EventBus;
|
||||
use crate::event_bus::EventBusInput;
|
||||
use futures::channel::mpsc::Sender;
|
||||
use futures::SinkExt;
|
||||
use futures::StreamExt;
|
||||
use reqwasm::websocket::futures::WebSocket;
|
||||
use reqwasm::websocket::Message;
|
||||
use std::time::Duration;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use yew_agent::Dispatched;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WebsocketService {
|
||||
// No messages sent from app to server on websocket at the moment.
|
||||
pub tx: Sender<()>,
|
||||
pub tx: Sender<common::WebSocketMessageAppToServer>,
|
||||
}
|
||||
|
||||
impl WebsocketService {
|
||||
@@ -34,16 +35,34 @@ impl WebsocketService {
|
||||
let ws = WebSocket::open(&ws_url).unwrap();
|
||||
log::info!("Opened websocket connection to {ws_url}");
|
||||
|
||||
let (_write, mut read) = ws.split();
|
||||
let (write, mut read) = ws.split();
|
||||
|
||||
let (in_tx, mut in_rx) = futures::channel::mpsc::channel::<()>(1000);
|
||||
let mut event_bus = EventBus::<common::WebSocketMessage>::dispatcher();
|
||||
let (in_tx, in_rx) = futures::channel::mpsc::channel::<common::WebSocketMessageAppToServer>(1000);
|
||||
let mut event_bus = EventBus::<common::State>::dispatcher();
|
||||
|
||||
// Generate regular heartbeat messages from app to server
|
||||
let (mut heartbeat_tx, heartbeat_rx) = futures::channel::mpsc::channel(1);
|
||||
spawn_local(async move {
|
||||
let heartbeat_interval = yew::platform::time::interval(Duration::from_secs(20));
|
||||
futures::pin_mut!(heartbeat_interval);
|
||||
while heartbeat_interval.next().await.is_some() {
|
||||
let msg = common::WebSocketMessageAppToServer::HeartBeat;
|
||||
heartbeat_tx.send(msg).await.unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
// App to Server
|
||||
let all_tx = futures::stream::select(in_rx, heartbeat_rx);
|
||||
spawn_local(async move {
|
||||
while let Some(()) = in_rx.next().await {
|
||||
// write.send(Message::Text(s)).await.unwrap();
|
||||
}
|
||||
// Serialize as JSON and map to websocket string message
|
||||
let all_tx = all_tx
|
||||
.map(|msg| serde_json::to_string(&msg).expect("Serialization error"))
|
||||
.map(Message::Text)
|
||||
.map(Ok);
|
||||
all_tx
|
||||
.forward(write)
|
||||
.await
|
||||
.expect("Forward to websocket write half failed");
|
||||
});
|
||||
|
||||
// Server to App
|
||||
@@ -51,11 +70,14 @@ impl WebsocketService {
|
||||
while let Some(msg) = read.next().await {
|
||||
match msg {
|
||||
Ok(Message::Text(data)) => {
|
||||
match serde_json::from_str::<common::WebSocketMessage>(&data) {
|
||||
Ok(ws_msg) => {
|
||||
log::debug!("Received ws message. Dispatching to event bus.");
|
||||
event_bus.send(EventBusInput::EventBusMsg(ws_msg));
|
||||
}
|
||||
match serde_json::from_str::<common::WebSocketMessageServerToApp>(&data) {
|
||||
Ok(ws_msg) => match ws_msg {
|
||||
common::WebSocketMessageServerToApp::State(state) => {
|
||||
log::debug!("Received ws message. Dispatching to event bus.");
|
||||
event_bus.send(EventBusInput::EventBusMsg(state));
|
||||
}
|
||||
common::WebSocketMessageServerToApp::HeartBeat => {}
|
||||
},
|
||||
Err(err) => {
|
||||
log::error!("{err:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user