From 0021c0be794e239e763c4d2869ee243b5adfd3f5 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:21:05 -0400 Subject: [PATCH] chat --- client/src/components/websocket.rs | 225 +++++++++++++++++++++-------- client/src/pages/home.rs | 2 +- server/src/api.rs | 2 +- 3 files changed, 166 insertions(+), 63 deletions(-) diff --git a/client/src/components/websocket.rs b/client/src/components/websocket.rs index 5405e2e..7a76679 100644 --- a/client/src/components/websocket.rs +++ b/client/src/components/websocket.rs @@ -1,9 +1,37 @@ -use html::Textarea; +use std::rc::Rc; + +use html::{Input, Textarea}; use leptos::*; use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn}; use lib::models::*; use serde_json::to_string; +#[derive(Clone)] +pub struct WebSocketContext { + pub ready_state: Signal, + pub message: Signal>, + send: Rc, +} + +impl WebSocketContext { + pub fn new( + ready_state: Signal, + message: Signal>, + send: Rc, + ) -> Self { + Self { + ready_state, + message, + send, + } + } + + #[inline(always)] + pub fn send(&self, message: &str) { + (self.send)(message) + } +} + #[component] pub fn Websocket() -> impl IntoView { let UseWebsocketReturn { @@ -15,10 +43,15 @@ pub fn Websocket() -> impl IntoView { .. } = use_websocket("ws://0.0.0.0:3030/websocket"); + provide_context(WebSocketContext::new( + ready_state, + message, + Rc::new(send.clone()), + )); + // Signals let (online_users, set_online_users) = create_signal(0); let (active_games, set_active_games) = create_signal(0); - let (chat_history, set_chat_history) = create_signal::>(vec![]); // Websocket stuff let status = move || ready_state.get().to_string(); @@ -37,23 +70,16 @@ pub fn Websocket() -> impl IntoView { packs: vec![0], }; - // Game stuff - let new_game_test = move |_| { - send(&to_string(&fake_new_game_request).unwrap()); - }; - let close_connection = move |_| { close(); set_online_users(0); set_active_games(0); - update_chat_history(&set_chat_history, format!("Disconnected.\n")); }; - // Chat stuff - let chat_history_ref = create_node_ref:: -
- -
- // -
- - +
+ +
+ + + } +} + +#[component] +pub fn Chat() -> impl IntoView { + let websocket = expect_context::(); + + // Chat stuff + let (chat_history, set_chat_history) = create_signal::>(vec![]); + let chat_history_ref = create_node_ref:: +
+ + + + +
+ + } +} + +#[component] +pub fn Auth() -> impl IntoView { + let (username, _set_username) = create_signal("Anonymous"); + + view! { +
+

Sign in:

+

Username:

+
} } diff --git a/client/src/pages/home.rs b/client/src/pages/home.rs index 049095d..0d5418e 100644 --- a/client/src/pages/home.rs +++ b/client/src/pages/home.rs @@ -27,7 +27,7 @@ pub fn Home() -> impl IntoView {

"Cards For Humanity"

- git + git
} diff --git a/server/src/api.rs b/server/src/api.rs index 6004713..304568e 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -15,7 +15,7 @@ use crate::message_handler::*; fn motd() -> String { format!( - "Welcome!" + "Greetings from the game server!" ) }