cards/client/src/components/websocket.rs

146 lines
4.3 KiB
Rust
Raw Normal View History

2024-07-23 22:21:05 -04:00
use std::rc::Rc;
2024-07-19 03:11:14 -04:00
use leptos::*;
use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
2024-07-21 02:35:13 -04:00
use lib::models::*;
use serde_json::to_string;
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-23 22:21:05 -04:00
send: Rc<dyn Fn(&str)>,
}
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)>,
) -> Self {
Self {
ready_state,
2024-07-24 22:06:19 -04:00
// message,
2024-07-23 22:21:05 -04:00
send,
}
}
#[inline(always)]
pub fn send(&self, message: &str) {
(self.send)(message)
}
}
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-22 01:32:09 -04:00
// Signals
let (online_users, set_online_users) = create_signal(0);
let (active_games, set_active_games) = create_signal(0);
2024-07-19 03:11:14 -04:00
2024-07-22 01:32:09 -04:00
// Websocket stuff
2024-07-19 03:11:14 -04:00
let status = move || ready_state.get().to_string();
let connected = move || ready_state.get() == ConnectionReadyState::Open;
let open_connection = move |_| {
open();
};
2024-07-24 22:06:19 -04:00
2024-07-21 02:35:13 -04:00
let fake_new_game_request = NewGameRequest {
name: String::from("Ligma"),
2024-07-21 22:48:15 -04:00
host: Player {
2024-07-21 02:35:13 -04:00
name: String::from("Adam"),
role: PlayerRole::Host,
white: vec![],
black: vec![],
},
packs: vec![0],
};
2024-07-22 01:32:09 -04:00
let close_connection = move |_| {
close();
set_online_users(0);
set_active_games(0);
};
2024-07-19 03:11:14 -04:00
2024-07-23 22:21:05 -04:00
// Game stuff
let new_game_test = move |_| {
send(&to_string(&fake_new_game_request).unwrap());
};
2024-07-19 03:11:14 -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);
let (game_object, set_game_object) = create_signal::<Option<Game>>(Option::None);
let (chat_message, set_chat_message) = create_signal::<Option<ChatMessage>>(Option::None);
2024-07-21 22:48:15 -04:00
2024-07-24 22:06:19 -04:00
provide_context::<ReadSignal<Option<Game>>>(game_object);
provide_context::<ReadSignal<Option<ChatMessage>>>(chat_message);
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-07-24 22:06:19 -04:00
if let Ok(game) = serde_json::from_str::<Game>(message) {
set_game_object(Some(game));
2024-07-22 01:32:09 -04:00
} else if let Ok(state_summary) =
serde_json::from_str::<ServerStateSummary>(message)
{
2024-07-24 22:06:19 -04:00
set_state_summary(Some(state_summary));
} else if let Ok(chat_message) = serde_json::from_str::<ChatMessage>(message) {
set_chat_message(Some(chat_message));
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-24 22:06:19 -04:00
// Update server info -> move this to a new component
create_effect(move |_| {
state_summary.with(move |state_summary| {
if let Some(state_summary) = state_summary {
set_online_users(state_summary.online_users);
set_active_games(state_summary.active_games);
}
})
});
2024-07-19 03:11:14 -04:00
view! {
2024-07-23 22:45:45 -04:00
<div class="w-auto">
2024-07-19 03:11:14 -04:00
<hr/>
2024-07-23 22:21:05 -04:00
<h2 class="p-1 text-2xl">Server Info:</h2>
2024-07-23 22:57:16 -04:00
<p class="p-1">"Connection Status: " {status}</p>
2024-07-22 01:32:09 -04:00
<p class="p-1">"Users Online: " {online_users}</p>
<p class="p-1">"Active Games: " {active_games}</p>
2024-07-19 03:11:14 -04:00
<div class="p-1">
<button on:click=open_connection disabled=connected>
2024-07-21 02:35:13 -04:00
"Connect"
2024-07-19 03:11:14 -04:00
</button>
<button on:click=close_connection disabled=move || !connected()>
2024-07-21 02:35:13 -04:00
"Disconnect"
2024-07-19 03:11:14 -04:00
</button>
2024-07-23 22:45:45 -04:00
<button on:click=new_game_test disabled=move || !connected()>
"Test New Game"
</button>
2024-07-19 03:11:14 -04:00
</div>
2024-07-23 22:21:05 -04:00
</div>
}
}