Struct wasm_sockets::EventClient
source · [−]pub struct EventClient {
pub url: Rc<RefCell<String>>,
pub status: Rc<RefCell<ConnectionStatus>>,
pub on_error: Rc<RefCell<Option<Box<dyn Fn(ErrorEvent)>>>>,
pub on_connection: Rc<RefCell<Option<Box<dyn Fn(&EventClient)>>>>,
pub on_message: Rc<RefCell<Option<Box<dyn Fn(&EventClient, Message)>>>>,
pub on_close: Rc<RefCell<Option<Box<dyn Fn()>>>>,
/* private fields */
}Fields
url: Rc<RefCell<String>>The URL this client is connected to
status: Rc<RefCell<ConnectionStatus>>The current connection status
on_error: Rc<RefCell<Option<Box<dyn Fn(ErrorEvent)>>>>The function bound to the on_error event
on_connection: Rc<RefCell<Option<Box<dyn Fn(&EventClient)>>>>The function bound to the on_connection event
on_message: Rc<RefCell<Option<Box<dyn Fn(&EventClient, Message)>>>>The function bound to the on_message event
on_close: Rc<RefCell<Option<Box<dyn Fn()>>>>The function bound to the on_close event
Implementations
Create a new EventClient and connect to a WebSocket URL
Note: An Ok() from this function does not mean the connection has succeeded.
EventClient::new("wss://ws.ifelse.io")?;Set an on_error event handler. This handler will be run when the client disconnects from the server due to an error. This will overwrite the previous handler. You can set None to disable the on_error handler.
client.set_on_error(Some(Box::new(|error| {
panic!("Error: {:#?}", error);
})));Set an on_connection event handler. This handler will be run when the client successfully connects to a server. This will overwrite the previous handler. You can set None to disable the on_connection handler.
client.set_on_connection(Some(Box::new(|client| {
info!("Connected");
})));Set an on_message event handler. This handler will be run when the client receives a message from a server. This will overwrite the previous handler. You can set None to disable the on_message handler.
client.set_on_message(Some(Box::new(
|c, m| {
info!("New Message: {:#?}", m);
},
)));Set an on_close event handler. This handler will be run when the client disconnects from a server without an error. This will overwrite the previous handler. You can set None to disable the on_close handler.
client.set_on_close(Some(Box::new(|| {
info!("Closed");
})));Send a text message to the server
client.send_string("Hello server!")?;