2024-07-19 03:11:14 -04:00
|
|
|
use leptos::*;
|
|
|
|
use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
|
2024-08-04 18:14:10 -04:00
|
|
|
use lib::*;
|
2024-07-24 22:37:18 -04:00
|
|
|
use serde_json::from_str;
|
2024-08-04 03:13:34 -04:00
|
|
|
use std::rc::Rc;
|
2024-07-19 03:11:14 -04:00
|
|
|
|
2024-07-23 22:21:05 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WebSocketContext {
|
|
|
|
pub ready_state: Signal<ConnectionReadyState>,
|
2024-07-24 22:06:19 -04:00
|
|
|
// pub message: Signal<Option<String>>,
|
2024-07-24 22:37:18 -04:00
|
|
|
pub send: Rc<dyn Fn(&str)>,
|
|
|
|
pub open: Rc<dyn Fn()>,
|
|
|
|
pub close: Rc<dyn Fn()>,
|
2024-07-23 22:21:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WebSocketContext {
|
|
|
|
pub fn new(
|
|
|
|
ready_state: Signal<ConnectionReadyState>,
|
2024-07-24 22:06:19 -04:00
|
|
|
// message: Signal<Option<String>>,
|
2024-07-23 22:21:05 -04:00
|
|
|
send: Rc<dyn Fn(&str)>,
|
2024-07-24 22:37:18 -04:00
|
|
|
open: Rc<dyn Fn()>,
|
|
|
|
close: Rc<dyn Fn()>,
|
2024-07-23 22:21:05 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
ready_state,
|
2024-07-24 22:06:19 -04:00
|
|
|
// message,
|
2024-07-23 22:21:05 -04:00
|
|
|
send,
|
2024-07-24 22:37:18 -04:00
|
|
|
open,
|
|
|
|
close,
|
2024-07-23 22:21:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn send(&self, message: &str) {
|
|
|
|
(self.send)(message)
|
|
|
|
}
|
2024-07-24 22:37:18 -04:00
|
|
|
|
2024-08-04 18:14:10 -04:00
|
|
|
// #[inline(always)]
|
|
|
|
// pub fn open(&self) {
|
|
|
|
// (self.open)()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[inline(always)]
|
|
|
|
// pub fn close(&self) {
|
|
|
|
// (self.close)()
|
|
|
|
// }
|
2024-07-23 22:21:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-19 03:11:14 -04:00
|
|
|
#[component]
|
|
|
|
pub fn Websocket() -> impl IntoView {
|
|
|
|
let UseWebsocketReturn {
|
|
|
|
ready_state,
|
|
|
|
message,
|
|
|
|
send,
|
|
|
|
open,
|
|
|
|
close,
|
|
|
|
..
|
|
|
|
} = use_websocket("ws://0.0.0.0:3030/websocket");
|
|
|
|
|
2024-07-23 22:21:05 -04:00
|
|
|
provide_context(WebSocketContext::new(
|
|
|
|
ready_state,
|
2024-07-24 22:06:19 -04:00
|
|
|
// message,
|
2024-07-23 22:21:05 -04:00
|
|
|
Rc::new(send.clone()),
|
2024-07-24 22:37:18 -04:00
|
|
|
Rc::new(open.clone()),
|
|
|
|
Rc::new(close.clone()),
|
2024-07-23 22:21:05 -04:00
|
|
|
));
|
|
|
|
|
2024-07-24 22:06:19 -04:00
|
|
|
// Contexts for message handler
|
|
|
|
let (state_summary, set_state_summary) =
|
|
|
|
create_signal::<Option<ServerStateSummary>>(Option::None);
|
2024-08-02 02:35:31 -04:00
|
|
|
// let (game_object, set_game_object) = create_signal::<Option<Game>>(Option::None);
|
2024-07-27 04:34:15 -04:00
|
|
|
let (user_update, set_user_update) = create_signal::<Option<UserUpdate>>(Option::None);
|
2024-07-28 01:38:32 -04:00
|
|
|
let (chat_update, set_chat_update) = create_signal::<Option<ChatUpdate>>(Option::None);
|
2024-07-24 22:06:19 -04:00
|
|
|
let (chat_message, set_chat_message) = create_signal::<Option<ChatMessage>>(Option::None);
|
2024-07-28 02:36:04 -04:00
|
|
|
let (active_games, set_active_games) = create_signal::<Option<GamesUpdate>>(Option::None);
|
2024-08-04 03:13:34 -04:00
|
|
|
let (card_packs_meta, set_card_packs_meta) = create_signal::<CardPacksMeta>(CardPacksMeta {
|
|
|
|
official_meta: vec![],
|
|
|
|
unofficial_meta: vec![],
|
|
|
|
});
|
2024-07-21 22:48:15 -04:00
|
|
|
|
2024-08-02 02:35:31 -04:00
|
|
|
// provide_context::<ReadSignal<Option<Game>>>(game_object);
|
2024-07-27 04:34:15 -04:00
|
|
|
provide_context::<ReadSignal<Option<UserUpdate>>>(user_update);
|
2024-07-28 01:38:32 -04:00
|
|
|
provide_context::<ReadSignal<Option<ChatUpdate>>>(chat_update);
|
2024-07-24 22:06:19 -04:00
|
|
|
provide_context::<ReadSignal<Option<ChatMessage>>>(chat_message);
|
2024-07-28 02:36:04 -04:00
|
|
|
provide_context::<ReadSignal<Option<GamesUpdate>>>(active_games);
|
2024-08-04 03:13:34 -04:00
|
|
|
provide_context::<ReadSignal<CardPacksMeta>>(card_packs_meta);
|
2024-07-24 22:06:19 -04:00
|
|
|
provide_context::<ReadSignal<Option<ServerStateSummary>>>(state_summary);
|
|
|
|
|
|
|
|
// Message handler
|
2024-07-19 03:11:14 -04:00
|
|
|
create_effect(move |_| {
|
2024-07-21 22:48:15 -04:00
|
|
|
message.with(move |message_raw| {
|
2024-07-19 03:11:14 -04:00
|
|
|
// Send all messages as strings into chat box
|
|
|
|
|
2024-07-21 22:48:15 -04:00
|
|
|
if let Some(message) = message_raw {
|
2024-08-02 02:35:31 -04:00
|
|
|
// if let Ok(game) = from_str::<Game>(message) {
|
|
|
|
// set_game_object(Some(game));
|
|
|
|
// }
|
|
|
|
if let Ok(state_summary) = from_str::<ServerStateSummary>(message) {
|
2024-07-24 22:06:19 -04:00
|
|
|
set_state_summary(Some(state_summary));
|
2024-07-24 22:37:18 -04:00
|
|
|
} else if let Ok(chat_message) = from_str::<ChatMessage>(message) {
|
2024-07-24 22:06:19 -04:00
|
|
|
set_chat_message(Some(chat_message));
|
2024-07-27 04:34:15 -04:00
|
|
|
} else if let Ok(user_update) = from_str::<UserUpdate>(message) {
|
|
|
|
set_user_update(Some(user_update));
|
2024-07-28 01:38:32 -04:00
|
|
|
} else if let Ok(chat_update) = from_str::<ChatUpdate>(message) {
|
|
|
|
set_chat_update(Some(chat_update));
|
2024-07-28 02:36:04 -04:00
|
|
|
} else if let Ok(games_update) = from_str::<GamesUpdate>(message) {
|
|
|
|
set_active_games(Some(games_update));
|
2024-08-04 03:13:34 -04:00
|
|
|
} else if let Ok(packs_meta_update) = from_str::<CardPacksMeta>(message) {
|
|
|
|
set_card_packs_meta(packs_meta_update);
|
2024-07-21 22:48:15 -04:00
|
|
|
} else {
|
2024-07-24 22:06:19 -04:00
|
|
|
logging::log!("Unhandled message: {}", message);
|
2024-07-19 03:11:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2024-07-23 22:21:05 -04:00
|
|
|
}
|