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§

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(|_evt| {
    info!("Closed");
})));

Send a text message to the server

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

Send a binary message to the server

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

Close the connection

client.close()?;

Close the connection with a custom close code and, optionally, a reason string

The reason string must be at most 123 bytes long.

client.close_with(1001, Some("going away"))?;

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.