Struct WebSocket

Source
pub struct WebSocket { /* private fields */ }

Implementations§

Source§

impl WebSocket

Source

pub fn new() -> Option<WebSocket>

Create a new WebSocket handler object. One object should be created by socket.

§Examples
let mut ws = WebSocket::new().unwrap();
ws.connect("wss://echo.websocket.org/");
Source

pub fn connect<T>(&mut self, url: T) -> bool
where T: AsRef<str>,

The connect method open a socket connection to the provided URL. This method should only be call if the current state of the handler is Closed, else the function will do nothing and return false.

§Examples
let mut ws = WebSocket::new().unwrap();
ws.connect("wss://echo.websocket.org/");
Source

pub fn get_id(&self) -> i32

Get id field of websocket.

Source

pub fn get_state(&self) -> WebSocketState

Get current websocket state.

Source

pub fn get_buffered_amount(&self) -> usize

Get the WebSocket.bufferedAmount field into bufferedAmount.

Source

pub fn get_url(&self) -> String

Get the websocket URL field.

Source

pub fn get_protocol(&self) -> String

Get the websocket protocol field.

Source

pub fn clear_internal_callback(&mut self)

Clear all internal callbacks for current object.

Source

pub fn set_open_callback(&mut self, cb: Option<fn(&mut Self)>)

Set on open callback for current object.

Source

pub fn set_error_callback(&mut self, cb: Option<fn(&mut Self)>)

Set on error callback for current object.

Source

pub fn set_close_callback(&mut self, cb: Option<fn(&mut Self)>)

Set on close callback for current object.

Source

pub fn set_message_callback(&mut self, cb: Option<fn(&mut Self, WebSocketData)>)

Set on message callback for current object.

Source

pub fn send_utf8_text<T>(&mut self, string: T) -> bool
where T: AsRef<str>,

Send UTF-8 formatted string through websocket. The state of current socket should be opened else the function will return false.

§Examples
let mut ws = WebSocket::new().unwrap();
ws.connect("wss://echo.websocket.org/");
ws.send_utf8_text("foo");
Source

pub fn send_binary(&mut self, data: &mut [u8]) -> bool

Send UTF-8 raw data through websocket. The state of current socket should be opened else the function will return false.

§Examples
let mut ws = WebSocket::new().unwrap();
ws.connect("wss://echo.websocket.org/");
ws.send_binary([42, 69]);
Source

pub fn close<T>(&mut self, code: u16, reason: T)
where T: AsRef<str>,

Close the current websocket. This function will clear all callbacks and trigger on close callback.

§Examples
let mut ws = WebSocket::new().unwrap();
ws.connect("wss://echo.websocket.org/");
ws.close(1000, "over");

Trait Implementations§

Source§

impl Drop for WebSocket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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.