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§

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.

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

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

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

Verify the WsState of the connection.

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.

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.

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.

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.

Retrieve the address to which this socket is connected.

Trait Implementations§

Formats the value using the given formatter. Read more
The error type that is returned if observing is not possible. Read more
Add an observer to the observable. Options allow chosing the channel type and to filter events with a predicate.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.