Added websocket implementation for the client
This commit is contained in:
parent
71c5bfd1f8
commit
465f4a5673
@ -27,6 +27,7 @@ cfg-if = "1.0.0"
|
|||||||
once_cell = "1.19.0"
|
once_cell = "1.19.0"
|
||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
uuid = "1.8.0"
|
uuid = "1.8.0"
|
||||||
|
leptos-use = "0.10.6"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
|
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
|
||||||
|
@ -16,6 +16,7 @@ pub fn App() -> impl IntoView {
|
|||||||
|
|
||||||
provide_context(participants);
|
provide_context(participants);
|
||||||
util::surrealdb::init_participants();
|
util::surrealdb::init_participants();
|
||||||
|
util::websocket::client::init_websocket();
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
// injects a stylesheet into the document <head>
|
// injects a stylesheet into the document <head>
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_router::ActionForm;
|
use leptos_router::ActionForm;
|
||||||
|
|
||||||
|
use crate::util::surrealdb::schemas::Participant;
|
||||||
|
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(feature = "ssr")] {
|
if #[cfg(feature = "ssr")] {
|
||||||
use crate::util::surrealdb::{DB, schemas};
|
use crate::util::surrealdb::{DB, schemas};
|
||||||
use crate::util::websocket::server;
|
use crate::util::websocket::{server, ParticipantsAction};
|
||||||
use leptos::logging;
|
use leptos::logging;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[server(AddParticipant)]
|
#[server(AddParticipant)]
|
||||||
async fn add_participant(name: String, group: String) -> Result<(), ServerFnError> {
|
async fn add_participant(name: String, group: String) -> Result<(), ServerFnError> {
|
||||||
let websocket_sate = &server::WEBSOCKET_STATE;
|
let websocket_state = &server::WEBSOCKET_STATE;
|
||||||
|
|
||||||
websocket_sate.tx.send("BOE".to_string()).unwrap();
|
|
||||||
|
|
||||||
let created: Vec<schemas::ParticipantRecord> = DB
|
let created: Vec<schemas::ParticipantRecord> = DB
|
||||||
.create("participant")
|
.create("participant")
|
||||||
@ -27,7 +27,20 @@ async fn add_participant(name: String, group: String) -> Result<(), ServerFnErro
|
|||||||
participant.name,
|
participant.name,
|
||||||
participant.group
|
participant.group
|
||||||
);
|
);
|
||||||
Ok(())
|
|
||||||
|
let action = ParticipantsAction::Add {
|
||||||
|
participant: Participant {
|
||||||
|
name: participant.name.clone(),
|
||||||
|
group: participant.group.clone(),
|
||||||
|
id: participant.id.to_string(),
|
||||||
|
events: None,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
match websocket_state.apply(action) {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(_) => Err(ServerFnError::new("Error sending websocket action")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => Err(ServerFnError::ServerError(String::from(
|
None => Err(ServerFnError::ServerError(String::from(
|
||||||
"Could not create participant",
|
"Could not create participant",
|
||||||
|
@ -45,5 +45,6 @@ pub fn AddTime() -> impl IntoView {
|
|||||||
<input type="text" name="name" autocomplete="off" />
|
<input type="text" name="name" autocomplete="off" />
|
||||||
<input type="submit" value="Tijd toevoegen" />
|
<input type="submit" value="Tijd toevoegen" />
|
||||||
</ActionForm>
|
</ActionForm>
|
||||||
|
<p>{ move || format!("{:?}", participants.unwrap().get()) }</p>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,36 @@
|
|||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|
||||||
|
pub mod client;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::util::surrealdb::schemas::Participant;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum ParticipantsAction {
|
||||||
|
Add { participant: Participant },
|
||||||
|
Replace { participant: Participant },
|
||||||
|
Remove { id: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
pub fn apply_participants_patch(mut participants: Vec<Participant>, action: ParticipantsAction) {
|
||||||
|
match action {
|
||||||
|
ParticipantsAction::Add { participant } => {
|
||||||
|
participants.push(participant);
|
||||||
|
}
|
||||||
|
ParticipantsAction::Remove { id } => {
|
||||||
|
participants.retain(|participant| participant.id != id);
|
||||||
|
}
|
||||||
|
ParticipantsAction::Replace { participant } => {
|
||||||
|
if let Some(index) = participants
|
||||||
|
.iter()
|
||||||
|
.position(|item| item.id == participant.id)
|
||||||
|
{
|
||||||
|
let _ = std::mem::replace(&mut participants[index], participant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
55
application/src/util/websocket/client.rs
Normal file
55
application/src/util/websocket/client.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use crate::util::surrealdb::schemas;
|
||||||
|
use leptos::*;
|
||||||
|
use leptos_use::*;
|
||||||
|
|
||||||
|
use super::ParticipantsAction;
|
||||||
|
|
||||||
|
pub fn init_websocket() {
|
||||||
|
let UseWebsocketReturn {
|
||||||
|
ready_state,
|
||||||
|
message,
|
||||||
|
..
|
||||||
|
} = use_websocket("ws://localhost:3000/ws");
|
||||||
|
|
||||||
|
create_effect(move |prev_value| {
|
||||||
|
let msg = message.get();
|
||||||
|
|
||||||
|
if prev_value != Some(msg.clone()) {
|
||||||
|
match msg {
|
||||||
|
Some(ref text) => handle_message(text),
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
msg
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_message(message: &str) {
|
||||||
|
let action: ParticipantsAction = match serde_json::from_str(message) {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(_err) => {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let participants_context = use_context::<RwSignal<Vec<schemas::Participant>>>().unwrap();
|
||||||
|
|
||||||
|
match action {
|
||||||
|
ParticipantsAction::Add { participant } => {
|
||||||
|
participants_context.update(|participants| participants.push(participant))
|
||||||
|
}
|
||||||
|
ParticipantsAction::Remove { id } => participants_context
|
||||||
|
.update(|participants| participants.retain(|participant| participant.id != id)),
|
||||||
|
ParticipantsAction::Replace { participant } => {
|
||||||
|
participants_context.update(|participants| {
|
||||||
|
if let Some(index) = participants
|
||||||
|
.iter()
|
||||||
|
.position(|item| item.id == participant.id)
|
||||||
|
{
|
||||||
|
let _ = std::mem::replace(&mut participants[index], participant);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::util::websocket;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::ws::{Message, WebSocket, WebSocketUpgrade},
|
extract::ws::{Message, WebSocket, WebSocketUpgrade},
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
@ -23,6 +24,15 @@ pub struct WebSocketState {
|
|||||||
pub tx: broadcast::Sender<String>,
|
pub tx: broadcast::Sender<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WebSocketState {
|
||||||
|
pub fn apply(&self, action: websocket::ParticipantsAction) -> Result<(), serde_json::Error> {
|
||||||
|
let message = serde_json::to_string(&action)?;
|
||||||
|
let _ = self.tx.send(message);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, axum::extract::FromRef)]
|
#[derive(Clone, axum::extract::FromRef)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub leptos_options: LeptosOptions,
|
pub leptos_options: LeptosOptions,
|
||||||
|
Loading…
Reference in New Issue
Block a user