Struct 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

Source

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")?;
Source

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

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

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

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

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

Send a text message to the server

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

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

Send a binary message to the server

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

pub fn close(&self) -> Result<(), JsValue>

Close the connection

client.close()?;
Source

pub fn close_with(&self, code: u16, reason: Option<&str>) -> Result<(), JsValue>

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.