websocket cleanup

This commit is contained in:
Maccesch 2023-07-13 11:25:32 +01:00
parent ddbc1c1db5
commit a0004a4559

View file

@ -26,48 +26,50 @@ use crate::utils::CloneableFnWithArg;
/// # #[component] /// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView { /// # fn Demo(cx: Scope) -> impl IntoView {
/// let UseWebsocketReturn { /// let UseWebsocketReturn {
/// ready_state, /// ready_state,
/// message, /// message,
/// message_bytes, /// message_bytes,
/// send, /// send,
/// send_bytes, /// send_bytes,
/// open, /// open,
/// close, /// close,
/// .. /// ..
/// } = use_websocket(cx, "wss://echo.websocket.events/".to_string()); /// } = use_websocket(cx, "wss://echo.websocket.events/".to_string());
/// ///
/// let send_message = move |_| { /// let send_message = move |_| {
/// let m = "Hello, world!".to_string(); /// let m = "Hello, world!".to_string();
/// send(m.clone()); /// send(m.clone());
/// }; /// };
/// ///
/// let send_byte_message = move |_| { /// let send_byte_message = move |_| {
/// let m = b"Hello, world!\r\n".to_vec(); /// let m = b"Hello, world!\r\n".to_vec();
/// send_bytes(m.clone()); /// send_bytes(m.clone());
/// }; /// };
/// ///
/// let status = move || ready_state().to_string(); /// let status = move || ready_state.get().to_string();
/// ///
/// let connected = move || ready_state.get() == UseWebSocketReadyState::Open; /// let connected = move || ready_state.get() == UseWebSocketReadyState::Open;
/// ///
/// let open_connection = move |_| { /// let open_connection = move |_| {
/// open(); /// open();
/// }; /// };
/// ///
/// let close_connection = move |_| { /// let close_connection = move |_| {
/// close(); /// close();
/// }; /// };
/// ///
/// view! { cx, /// view! { cx,
/// <div> /// <div>
/// <p>"status: " {status}</p> /// <p>"status: " {status}</p>
/// button on:click=send_message disabled=move || !connected()>"Send"</button> ///
/// <button on:click=send_byte_message disabled=move || !connected()>"Send bytes"</button> /// <button on:click=send_message disabled=move || !connected()>"Send"</button>
/// <button on:click=open_connection disabled=connected>"Open"</button> /// <button on:click=send_byte_message disabled=move || !connected()>"Send bytes"</button>
/// <button on:click=close_connection disabled=move || !connected()>"Close"</button> /// <button on:click=open_connection disabled=connected>"Open"</button>
/// <p>"Receive message: " {format! {"{:?}", message}}</p> /// <button on:click=close_connection disabled=move || !connected()>"Close"</button>
/// <p>"Receive byte message: " {format! {"{:?}", message_bytes}}</p> ///
/// </div> /// <p>"Receive message: " {format! {"{:?}", message}}</p>
/// <p>"Receive byte message: " {format! {"{:?}", message_bytes}}</p>
/// </div>
/// } /// }
/// # } /// # }
/// ``` /// ```
@ -371,15 +373,15 @@ impl fmt::Display for UseWebSocketReadyState {
#[derive(DefaultBuilder)] #[derive(DefaultBuilder)]
pub struct UseWebSocketOptions { pub struct UseWebSocketOptions {
/// `WebSocket` connect callback. /// `WebSocket` connect callback.
on_open: Box<dyn CloneableFnWithArg<Event> + 'static>, on_open: Box<dyn CloneableFnWithArg<Event>>,
/// `WebSocket` message callback for text. /// `WebSocket` message callback for text.
on_message: Box<dyn CloneableFnWithArg<String> + 'static>, on_message: Box<dyn CloneableFnWithArg<String>>,
/// `WebSocket` message callback for binary. /// `WebSocket` message callback for binary.
on_message_bytes: Box<dyn CloneableFnWithArg<Vec<u8>> + 'static>, on_message_bytes: Box<dyn CloneableFnWithArg<Vec<u8>>>,
/// `WebSocket` error callback. /// `WebSocket` error callback.
on_error: Box<dyn CloneableFnWithArg<Event> + 'static>, on_error: Box<dyn CloneableFnWithArg<Event>>,
/// `WebSocket` close callback. /// `WebSocket` close callback.
on_close: Box<dyn CloneableFnWithArg<CloseEvent> + 'static>, on_close: Box<dyn CloneableFnWithArg<CloseEvent>>,
/// Retry times. /// Retry times.
reconnect_limit: Option<u64>, reconnect_limit: Option<u64>,
/// Retry interval(ms). /// Retry interval(ms).