2024-07-19 03:11:14 -04:00
|
|
|
use html::Textarea;
|
|
|
|
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
|
|
|
|
|
|
|
#[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-20 23:00:19 -04:00
|
|
|
// Websocket stuff
|
2024-07-21 02:35:13 -04:00
|
|
|
// let send_message = move |_| {
|
|
|
|
// send("Hello, world!");
|
|
|
|
// };
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
let close_connection = move |_| {
|
|
|
|
close();
|
|
|
|
};
|
|
|
|
|
2024-07-21 02:35:13 -04:00
|
|
|
let fake_new_game_request = NewGameRequest {
|
|
|
|
name: String::from("Ligma"),
|
|
|
|
host: CAHPlayer {
|
|
|
|
name: String::from("Adam"),
|
|
|
|
role: PlayerRole::Host,
|
|
|
|
white: vec![],
|
|
|
|
black: vec![],
|
|
|
|
},
|
|
|
|
packs: vec![0],
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_game_test = move |_| {
|
|
|
|
send(&to_string(&fake_new_game_request).unwrap());
|
|
|
|
};
|
|
|
|
|
2024-07-20 23:00:19 -04:00
|
|
|
// Chat stuff
|
2024-07-19 03:11:14 -04:00
|
|
|
let chat_history_ref = create_node_ref::<Textarea>();
|
|
|
|
|
|
|
|
let (chat_history, set_chat_history) = create_signal(vec![]);
|
|
|
|
|
|
|
|
fn update_chat_history(&history: &WriteSignal<Vec<String>>, message: String) {
|
|
|
|
let _ = &history.update(|history: &mut Vec<_>| history.push(message));
|
|
|
|
}
|
|
|
|
|
2024-07-21 02:35:13 -04:00
|
|
|
// fn message_handler(message) {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2024-07-19 03:11:14 -04:00
|
|
|
// handle incoming messages
|
|
|
|
create_effect(move |_| {
|
|
|
|
message.with(move |message| {
|
|
|
|
// Send all messages as strings into chat box
|
|
|
|
if let Some(m) = message {
|
|
|
|
update_chat_history(&set_chat_history, format!("{}\n", m));
|
|
|
|
|
|
|
|
// Scroll chat textarea to bottom
|
|
|
|
if let Some(hist) = chat_history_ref.get() {
|
|
|
|
hist.set_scroll_top(hist.scroll_height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2024-07-20 23:00:19 -04:00
|
|
|
// Login stuff
|
|
|
|
let (username, _set_username) = create_signal("ligma");
|
|
|
|
|
2024-07-19 03:11:14 -04:00
|
|
|
view! {
|
2024-07-20 23:00:19 -04:00
|
|
|
<div class="w-auto bg-slate-500">
|
2024-07-19 03:11:14 -04:00
|
|
|
<hr/>
|
2024-07-20 23:00:19 -04:00
|
|
|
<h2 class="text-2xl">Connection</h2>
|
2024-07-19 03:11:14 -04:00
|
|
|
<p class="p-1">"status: " {status}</p>
|
|
|
|
<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>
|
|
|
|
</div>
|
|
|
|
<hr/>
|
|
|
|
<div class="p-1">
|
2024-07-20 23:00:19 -04:00
|
|
|
<h2 class="text-2xl">Sign in</h2>
|
|
|
|
<p>Username:</p>
|
2024-07-19 03:11:14 -04:00
|
|
|
<input
|
2024-07-20 23:00:19 -04:00
|
|
|
class="w-96 font-mono rounded-sm bg-slate-900 text-slate-200"
|
|
|
|
placeholder={move || username.get()}
|
2024-07-19 03:11:14 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<hr />
|
|
|
|
<div class="p-1">
|
2024-07-20 23:00:19 -04:00
|
|
|
<h2 class="text-2xl">Chat</h2>
|
2024-07-19 03:11:14 -04:00
|
|
|
<textarea
|
|
|
|
node_ref=chat_history_ref
|
2024-07-20 23:00:19 -04:00
|
|
|
class="w-96 h-60 font-mono rounded-sm resize-none bg-slate-900 text-slate-200"
|
2024-07-19 03:11:14 -04:00
|
|
|
readonly=true
|
|
|
|
wrap="soft"
|
|
|
|
>
|
|
|
|
{move || chat_history.get()}
|
|
|
|
</textarea>
|
|
|
|
<br/>
|
|
|
|
<input
|
2024-07-20 23:00:19 -04:00
|
|
|
class="w-96 font-mono rounded-sm bg-slate-900 text-slate-200"
|
2024-07-19 03:11:14 -04:00
|
|
|
placeholder="talk shit..."
|
|
|
|
/>
|
|
|
|
<br/>
|
2024-07-21 02:35:13 -04:00
|
|
|
// <button on:click=send_message disabled=move || !connected()>
|
|
|
|
// "Send"
|
|
|
|
// </button>
|
|
|
|
<br />
|
|
|
|
<button on:click=new_game_test disabled=move || !connected()>
|
|
|
|
"Test New Game"
|
2024-07-19 03:11:14 -04:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<hr/>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|