1use dioxus_devtools::DevserverMsg;
2use wasm_bindgen::{prelude::Closure, JsCast};
3use web_sys::{js_sys::JsString, MessageEvent, WebSocket};
4
5pub fn connect_to_hot_patch_messages() {
14 let location = web_sys::window().unwrap().location();
17 let url = format!(
18 "{protocol}//{host}/_dioxus?build_id={build_id}",
19 protocol = match location.protocol().unwrap() {
20 prot if prot == "https:" => "wss:",
21 _ => "ws:",
22 },
23 host = location.host().unwrap(),
24 build_id = dioxus_cli_config::build_id(),
25 );
26
27 let ws = WebSocket::new(&url).unwrap();
28
29 ws.set_onmessage(Some(
30 Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
31 let Ok(text) = e.data().dyn_into::<JsString>() else {
32 return;
33 };
34
35 let string: String = text.into();
37 let string = Box::leak(string.into_boxed_str());
38
39 if let Ok(DevserverMsg::HotReload(msg)) =
40 serde_json::from_str::<DevserverMsg>(string)
41 {
42 if let Some(jump_table) = msg.jump_table.as_ref().cloned() {
43 if msg.for_build_id == Some(dioxus_cli_config::build_id()) {
44 let our_pid = if cfg!(target_family = "wasm") {
45 None
46 } else {
47 Some(std::process::id())
48 };
49
50 if msg.for_pid == our_pid {
51 unsafe { subsecond::apply_patch(jump_table) }
52 .unwrap();
53 }
54 }
55 }
56 }
57 })
58 .into_js_value()
59 .as_ref()
60 .unchecked_ref(),
61 ));
62}