use leptos::*; use leptos_use::docs::demo_or_body; use leptos_use::websocket::*; #[component] fn Demo(cx: Scope) -> impl IntoView { let (history, set_history) = create_signal(cx, vec![]); fn update_history(&history: &WriteSignal>, message: String) { let _ = &history.update(|history: &mut Vec<_>| history.push(message)); } // ---------------------------- // use_websocket // ---------------------------- let UseWebsocketReturn { ready_state, message, message_bytes, send, send_bytes, open, close, .. } = use_websocket(cx, "wss://echo.websocket.events/".to_string()); let send_message = move |_| { let m = "Hello, world!".to_string(); send(m.clone()); set_history.update(|history: &mut Vec<_>| history.push(format! {"[send]: {:?}", m})); }; let send_byte_message = move |_| { let m = b"Hello, world!\r\n".to_vec(); send_bytes(m.clone()); set_history.update(|history: &mut Vec<_>| history.push(format! {"[send_bytes]: {:?}", m})); }; let status = move || ready_state().to_string(); let connected = move || ready_state.get() == UseWebSocketReadyState::Open; let open_connection = move |_| { open(); }; let close_connection = move |_| { close(); }; create_effect(cx, move |_| { if let Some(m) = message.get() { update_history(&set_history, format! {"[message]: {:?}", m}); }; }); create_effect(cx, move |_| { if let Some(m) = message_bytes.get() { update_history(&set_history, format! {"[message_bytes]: {:?}", m}); }; }); // ---------------------------- // use_websocket_with_options // ---------------------------- let (history2, set_history2) = create_signal(cx, vec![]); let UseWebsocketReturn { ready_state: ready_state2, send: send2, send_bytes: send_bytes2, open: open2, close: close2, message: message2, message_bytes: message_bytes2, .. } = use_websocket_with_options( cx, "wss://echo.websocket.events/".to_string(), UseWebSocketOptions { manual: true, onopen: Some(Box::new(move |e| { set_history2.update(|history: &mut Vec<_>| { history.push(format! {"[onopen]: event {:?}", e.type_()}) }); })), onclose: Some(Box::new(move |e| { set_history2.update(|history: &mut Vec<_>| { history.push(format! {"[onclose]: event {:?}", e.type_()}) }); })), onerror: Some(Box::new(move |e| { set_history2.update(|history: &mut Vec<_>| { history.push(format! {"[onerror]: event {:?}", e.type_()}) }); })), onmessage: Some(Box::new(move |m| { set_history2 .update(|history: &mut Vec<_>| history.push(format! {"[onmessage]: {:?}", m})); })), onmessage_bytes: Some(Box::new(move |m| { set_history2.update(|history: &mut Vec<_>| { history.push(format! {"[onmessage_bytes]: {:?}", m}) }); })), ..Default::default() }, ); let open_connection2 = move |_| { open2(); }; let close_connection2 = move |_| { close2(); }; let send_message2 = move |_| { let message = "Hello, use_leptos!".to_string(); send2(message.clone()); update_history(&set_history2, format! {"[send]: {:?}", message}); }; let send_byte_message2 = move |_| { let m = b"Hello, world!\r\n".to_vec(); send_bytes2(m.clone()); update_history(&set_history2, format! {"[send_bytes]: {:?}", m}); }; let status2 = move || ready_state2.get().to_string(); create_effect(cx, move |_| { if let Some(m) = message2.get() { update_history(&set_history2, format! {"[message]: {:?}", m}); }; }); create_effect(cx, move |_| { if let Some(m) = message_bytes2.get() { update_history(&set_history2, format! {"[message_bytes]: {:?}", m}); }; }); let connected2 = move || ready_state2.get() == UseWebSocketReadyState::Open; view! { cx,

"use_websocket"

"status: " {status}

"History"

{message}
} } />

"use_websocket_with_options"

"status: " {status2}

"History"

    {message} } } />
} } fn main() { _ = console_log::init_with_level(log::Level::Info); console_error_panic_hook::set_once(); mount_to(demo_or_body(), |cx| { view! { cx, } }) }