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(CloseEvent)>>>>,
/* 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(CloseEvent)>>>>
The function bound to the on_close event
Implementations§
source§impl EventClient
impl EventClient
sourcepub fn new(url: &str) -> Result<Self, WebSocketError>
pub fn new(url: &str) -> Result<Self, WebSocketError>
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")?;
sourcepub fn set_on_error(&mut self, f: Option<Box<dyn Fn(ErrorEvent)>>)
pub fn set_on_error(&mut self, f: Option<Box<dyn Fn(ErrorEvent)>>)
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);
})));
sourcepub fn set_on_connection(&mut self, f: Option<Box<dyn Fn(&EventClient)>>)
pub fn set_on_connection(&mut self, f: Option<Box<dyn Fn(&EventClient)>>)
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");
})));
sourcepub fn set_on_message(&mut self, f: Option<Box<dyn Fn(&EventClient, Message)>>)
pub fn set_on_message(&mut self, f: Option<Box<dyn Fn(&EventClient, Message)>>)
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);
},
)));
sourcepub fn set_on_close(&mut self, f: Option<Box<dyn Fn(CloseEvent)>>)
pub fn set_on_close(&mut self, f: Option<Box<dyn Fn(CloseEvent)>>)
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(|_evt| {
info!("Closed");
})));
sourcepub fn send_string(&self, message: &str) -> Result<(), JsValue>
pub fn send_string(&self, message: &str) -> Result<(), JsValue>
Send a text message to the server
client.send_string("Hello server!")?;