mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-14 16:36:07 +00:00
52 lines
1.8 KiB
Rust
52 lines
1.8 KiB
Rust
|
use wasm_bindgen::prelude::*;
|
||
|
use wasm_bindgen::JsCast;
|
||
|
use web_sys::{ErrorEvent, MessageEvent, WebSocket};
|
||
|
|
||
|
macro_rules! console_log {
|
||
|
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
|
||
|
}
|
||
|
|
||
|
#[wasm_bindgen]
|
||
|
extern "C" {
|
||
|
#[wasm_bindgen(js_namespace = console)]
|
||
|
fn log(s: &str);
|
||
|
}
|
||
|
|
||
|
#[wasm_bindgen(start)]
|
||
|
pub fn start_websocket() -> Result<(), JsValue> {
|
||
|
// Assuming, you run a WebSockets server at localhost:8081
|
||
|
// You'll also need to disable CORS in case of serving this example from
|
||
|
// the different `host:port` then your WebSockets server
|
||
|
let ws = WebSocket::new("ws://localhost:8081")
|
||
|
.expect("should create a socket");
|
||
|
|
||
|
// create callback
|
||
|
let onmessage_callback = Closure::wrap(Box::new(move |e: MessageEvent| {
|
||
|
// handle message
|
||
|
console_log!("message event, received data {:?}", e.data());
|
||
|
}) as Box<dyn FnMut(MessageEvent)>);
|
||
|
// set message event handler on WebSocket
|
||
|
ws.set_onmessage(Some(onmessage_callback.as_ref().unchecked_ref()));
|
||
|
// forget the callback to keep it alive
|
||
|
onmessage_callback.forget();
|
||
|
|
||
|
let onerror_callback = Closure::wrap(Box::new(move |e: ErrorEvent| {
|
||
|
console_log!("error event {:?}", e);
|
||
|
}) as Box<dyn FnMut(ErrorEvent)>);
|
||
|
ws.set_onerror(Some(onerror_callback.as_ref().unchecked_ref()));
|
||
|
onerror_callback.forget();
|
||
|
|
||
|
let cloned_ws = ws.clone();
|
||
|
let onopen_callback = Closure::wrap(Box::new(move |_| {
|
||
|
console_log!("socket opened");
|
||
|
match cloned_ws.send_with_str("ping") {
|
||
|
Ok(_) => console_log!("message sent successfully"),
|
||
|
Err(err) => console_log!("error sending message {:?}", err),
|
||
|
}
|
||
|
}) as Box<dyn FnMut(JsValue)>);
|
||
|
ws.set_onopen(Some(onopen_callback.as_ref().unchecked_ref()));
|
||
|
onopen_callback.forget();
|
||
|
|
||
|
Ok(())
|
||
|
}
|