Struct ws_stream_wasm::WsMeta
source · pub struct WsMeta { /* 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
impl WsMeta
sourcepub async fn connect(
url: impl AsRef<str>,
protocols: impl Into<Option<Vec<&str>>>
) -> Result<(Self, WsStream), WsErr>
pub async fn connect(
url: impl AsRef<str>,
protocols: impl Into<Option<Vec<&str>>>
) -> Result<(Self, WsStream), WsErr>
Connect to the server. The future will resolve when the connection has been established with a successful WebSocket handshake.
This returns both a WsMeta (allow manipulating and requesting meta data for the connection) and
a WsStream (Stream
/Sink
over WsMessage). WsStream can be wrapped to obtain
AsyncRead
/AsyncWrite
/AsyncBufRead
with WsStream::into_io.
Errors
Browsers will forbid making websocket connections to certain ports. See this Stack Overflow question.
connect
will return a WsErr::ConnectionFailed as it is indistinguishable from other connection failures.
If the URL is invalid, a WsErr::InvalidUrl is returned. See the HTML Living Standard for more information.
When the connection fails (server port not open, wrong ip, wss:// on ws:// server, … See the HTML Living Standard for details on all failure possibilities), a WsErr::ConnectionFailed is returned.
Note: Sending protocols to a server that doesn’t support them will make the connection fail.
sourcepub async fn close(&self) -> Result<CloseEvent, WsErr>
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
sourcepub async fn close_code(&self, code: u16) -> Result<CloseEvent, WsErr>
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
sourcepub async fn close_reason(
&self,
code: u16,
reason: impl AsRef<str>
) -> Result<CloseEvent, WsErr>
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
sourcepub fn ready_state(&self) -> WsState
pub fn ready_state(&self) -> WsState
Verify the WsState of the connection.
sourcepub fn wrapped(&self) -> &WebSocket
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.
sourcepub fn buffered_amount(&self) -> u32
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.
sourcepub fn extensions(&self) -> String
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.