Struct WebSocket

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

The meta data related to a websocket. Allows access to the methods on the WebSocket API. This is split from the Stream/Sink so you can pass the latter to a combinator whilst continuing to use this API.

A WsMeta instance is observable through the pharos::Observable trait. The type of event is WsEvent. In the case of a Close event, there will be additional information included as a CloseEvent.

When you drop this, the connection does not get closed, however when you drop WsStream it does.

Most of the methods on this type directly map to the web API. For more documentation, check the MDN WebSocket documentation.

Implementations§

Source§

impl WsMeta

Source

pub async fn connect(url: impl AsRef<str>) -> Result<(Self, WsStream), WsErr>

Connect to the server. The future will resolve when the connection has been established with a successful WebSocket handshake.

Source

pub async fn close(&self) -> Result<CloseEvent, WsErr>

Close the socket. The future will resolve once the socket’s state has become WsState::CLOSED. See: MDN Documentation

Source

pub async fn close_code(&self, code: u16) -> Result<CloseEvent, WsErr>

Close the socket. The future will resolve once the socket’s state has become WsState::CLOSED. See: MDN Documentation

Source

pub async fn close_reason( &self, code: u16, reason: impl AsRef<str>, ) -> Result<CloseEvent, WsErr>

Close the socket. The future will resolve once the socket’s state has become WsState::CLOSED. See: MDN Documentation

Source

pub fn ready_state(&self) -> WsState

Verify the WsState of the connection.

Source

pub fn wrapped(&self) -> &WebSocket

Access the wrapped web_sys::WebSocket directly.

ws_stream_wasm tries to expose all useful functionality through an idiomatic rust API, so hopefully you won’t need this, however if I missed something, you can.

§Caveats

If you call set_onopen, set_onerror, set_onmessage or set_onclose on this, you will overwrite the event listeners from ws_stream_wasm, and things will break.

Source

pub fn buffered_amount(&self) -> u32

The number of bytes of data that have been queued but not yet transmitted to the network.

NOTE: that this is the number of bytes buffered by the underlying platform WebSocket implementation. It does not reflect any buffering performed by ws_stream_wasm.

Source

pub fn extensions(&self) -> String

The extensions selected by the server as negotiated during the connection.

NOTE: This is an untested feature. The back-end server we use for testing (tungstenite) does not support Extensions.

Source

pub fn protocol(&self) -> String

The name of the sub-protocol the server selected during the connection.

This will be one of the strings specified in the protocols parameter when creating this WsMeta instance.

Source

pub fn url(&self) -> String

Retrieve the address to which this socket is connected.

Trait Implementations§

Source§

impl Debug for WsMeta

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Observable<WsEvent> for WsMeta

Source§

type Error = PharErr

The error type that is returned if observing is not possible. Read more
Source§

fn observe( &mut self, options: ObserveConfig<WsEvent>, ) -> Observe<'_, WsEvent, Self::Error>

Add an observer to the observable. Options allow chosing the channel type and to filter events with a predicate.

Auto Trait Implementations§

§

impl Freeze for WsMeta

§

impl !RefUnwindSafe for WsMeta

§

impl Send for WsMeta

§

impl Sync for WsMeta

§

impl Unpin for WsMeta

§

impl !UnwindSafe for WsMeta

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.