[][src]Struct wasm_sockets::EventClient

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()>>>>,
    // some fields omitted
}

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

impl EventClient[src]

pub fn new(url: &str) -> Result<Self, WebSocketError>[src]

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://echo.websocket.org")?;

pub fn set_on_error(&mut self, f: Option<Box<dyn Fn(ErrorEvent)>>)[src]

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);
})));

pub fn set_on_connection(&mut self, f: Option<Box<dyn Fn(&EventClient)>>)[src]

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");
})));

pub fn set_on_message(&mut self, f: Option<Box<dyn Fn(&EventClient, Message)>>)[src]

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);
    },
 )));

pub fn set_on_close(&mut self, f: Option<Box<dyn Fn()>>)[src]

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");
})));

pub fn send_string(&self, message: &str) -> Result<(), JsValue>[src]

Send a text message to the server

client.send_string("Hello server!")?;

pub fn send_binary(&self, message: Vec<u8>) -> Result<(), JsValue>[src]

Send a binary message to the server

client.send_binary(vec![0x2, 0xF])?;

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.