use wasm_bindgen::JsCast as _;
pub struct Websocket {
socket: web_sys::WebSocket,
#[allow(unused)]
on_message: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::MessageEvent)>,
#[allow(unused)]
on_open: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::Event)>,
#[allow(unused)]
on_error: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::Event)>,
#[allow(unused)]
on_close: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::Event)>,
}
impl Websocket {
pub fn new(uri: &str, proto: &str, mut on_message: impl 'static + FnMut(Vec<u8>), mut on_open: impl 'static + FnMut(), mut on_error: impl 'static + FnMut(), mut on_close: impl 'static + FnMut()) -> Result<Self, wasm_bindgen::JsValue> {
let socket = web_sys::WebSocket::new_with_str(uri, proto)?;
socket.set_binary_type(web_sys::BinaryType::Arraybuffer);
let on_message = wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |e: web_sys::MessageEvent| {
if let Ok(abuf) = e.data().dyn_into::<js_sys::ArrayBuffer>() {
let array = js_sys::Uint8Array::new(&abuf);
let vec = array.to_vec();
on_message(vec);
} else {
}
});
socket.set_onmessage(Some(on_message.as_ref().unchecked_ref()));
let on_open = wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |_: web_sys::Event| {
on_open()
});
socket.set_onopen(Some(on_open.as_ref().unchecked_ref()));
let on_error = wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |_: web_sys::Event| {
on_error()
});
socket.set_onerror(Some(on_error.as_ref().unchecked_ref()));
let on_close = wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |_: web_sys::Event| {
on_close()
});
socket.set_onclose(Some(on_close.as_ref().unchecked_ref()));
Ok(Websocket {
socket,
on_message,
on_open,
on_error,
on_close,
})
}
pub fn send_binary(&mut self, data: &[u8]) -> Result<(), wasm_bindgen::JsValue> {
self.socket.send_with_u8_array(data)
}
}