tqwewe / leptos_server_signal

Leptos server signals synced through websockets
MIT License
56 stars 12 forks source link

Get access to the websocket object to set additional callbacks #11

Closed messense closed 6 months ago

messense commented 9 months ago

For example I'd like to call set_onclose on it so I can display a warning on page when the websocket disconnects.

tqwewe commented 6 months ago

I've added support for this in 0.6.1.

The provide_websocket function now returns a web_sys::WebSocket. ServerSignalWebSocket is also public now, so you can use use_context::<ServerSignalWebSocket>().unwrap().ws() to get access to the websocket.

let websocket = provide_websocket("ws://localhost:3000/ws").unwrap();
if let Some(websocket) = websocket {
    websocket.set_onclose(Some(&js_sys::Function::new_no_args("alert('closed')")));
}