pub struct WebSocket { /* private fields */ }
Expand description
An async WebSocket implementation capable of operating uniformly under a browser-backed executor in WASM and under native tokio-runtime.
Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub fn new(
url: Option<&str>,
config: Option<WebSocketConfig>,
) -> Result<WebSocket>
pub fn new( url: Option<&str>, config: Option<WebSocketConfig>, ) -> Result<WebSocket>
Create a new WebSocket instance connecting to the given URL.
Sourcepub fn set_url(&self, url: &str)
pub fn set_url(&self, url: &str)
Changes WebSocket connection URL.
Following this call, you must invoke
WebSocket::reconnect().await
manually
Sourcepub fn configure(&self, config: WebSocketConfig)
pub fn configure(&self, config: WebSocketConfig)
Configure WebSocket connection settings Can be supplied after the WebSocket has been has been created to alter the configuration for the next connection.
Sourcepub fn receiver_rx(&self) -> &Receiver<Message>
pub fn receiver_rx(&self) -> &Receiver<Message>
Returns the reference to the Receiver channel
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true if websocket is connected, false otherwise
Sourcepub async fn connect(&self, options: ConnectOptions) -> ConnectResult<Error>
pub async fn connect(&self, options: ConnectOptions) -> ConnectResult<Error>
Connects the websocket to the destination URL.
Optionally accepts block_until_connected
argument
that will block the async execution until the websocket
is connected.
Once invoked, connection task will run in the background and will attempt to repeatedly reconnect if the websocket connection is closed.
To suspend reconnection, you have to call disconnect()
method explicitly.
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnects the websocket from the destination server.
Sourcepub async fn reconnect(&self) -> Result<()>
pub async fn reconnect(&self) -> Result<()>
Trigger WebSocket to reconnect. This method closes the underlying WebSocket connection causing the WebSocket implementation to re-initiate connection.
Sourcepub async fn post(&self, message: Message) -> Result<&Self>
pub async fn post(&self, message: Message) -> Result<&Self>
Sends a message to the destination server. This function will queue the message on the relay channel and return successfully if the message has been queued. This function enforces async yield in order to prevent potential blockage of the executor if it is being executed in tight loops.
Sourcepub async fn send(&self, message: Message) -> Result<&Self, Arc<Error>>
pub async fn send(&self, message: Message) -> Result<&Self, Arc<Error>>
Sends a message to the destination server. This function will block until until the message was relayed to the underlying websocket implementation.
Sourcepub async fn recv(&self) -> Result<Message>
pub async fn recv(&self) -> Result<Message>
Receives message from the websocket. Blocks until a message is received from the underlying websocket connection.
Sourcepub fn trigger_abort(&self) -> Result<()>
pub fn trigger_abort(&self) -> Result<()>
Triggers a disconnection on the underlying WebSocket. This is intended for debug purposes only. Can be used to test application reconnection logic.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for WebSocket
impl RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin for WebSocket
impl UnwindSafe for WebSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.