Struct ratchet_rs::WebSocket [−][src]
pub struct WebSocket<S, E> { /* fields omitted */ }
Expand description
An upgraded WebSocket stream.
This is created after a connection has been upgraded to speak the WebSocket protocol by using
the accept
, accept_with
, subscribe
and subscribe_with
functions or by using
WebSocket::from_upgraded
with an already upgraded stream.
Example
let stream = TcpStream::connect("127.0.0.1:9001").await?;
let upgraded = subscribe(WebSocketConfig::default(), stream, "ws://127.0.0.1/hello").await?;
let UpgradedClient { mut websocket, .. } = upgraded;
let mut buf = BytesMut::new();
loop {
match websocket.read(&mut buf).await? {
Message::Text => {
websocket.write(&mut buf, PayloadType::Text).await?;
buf.clear();
}
Message::Binary => {
websocket.write(&mut buf, PayloadType::Binary).await?;
buf.clear();
}
Message::Ping | Message::Pong => {}
Message::Close(_) => break Ok(()),
}
}
Implementations
pub fn from_upgraded(
config: WebSocketConfig,
stream: S,
extension: NegotiatedExtension<E>,
read_buffer: BytesMut,
role: Role
) -> WebSocket<S, E>
pub fn from_upgraded(
config: WebSocketConfig,
stream: S,
extension: NegotiatedExtension<E>,
read_buffer: BytesMut,
role: Role
) -> WebSocket<S, E>
Initialise a new WebSocket
from a stream that has already executed a handshake.
Arguments
config
- The configuration to initialise the WebSocket with.
stream
- The stream that the handshake was executed on.
extension
- A negotiated extension that will be used for the session.
read_buffer
- The read buffer which will be used for the session. This may contain any
unread data received after performing the handshake that was not required.
role
- The role that this WebSocket will take.
Attempt to read some data from the WebSocket. Returning either the type of the message received or the error that was produced.
Errors
If an error is produced during a read operation the contents of read_buffer
must be
considered to be dirty.
Note
Ratchet transparently handles ping messages received from the peer by returning a pong frame
and this function will return Message::Pong
if one has been received. As per RFC6455
these may be interleaved between data frames. In the event of one being received while
reading a continuation, this function will then yield Message::Ping
and the read_buffer
will contain the data received up to that point. The callee must ensure that the contents
of read_buffer
are not then modified before calling read
again.
Constructs a new text WebSocket message with a payload of data
.
Constructs a new binary WebSocket message with a payload of data
.
Constructs a new ping WebSocket message with a payload of data
.
Constructs a new WebSocket message of message_type
and with a payload of `buf.
Close this WebSocket with the reason provided.
Close this WebSocket with the reason provided.
Constructs a new WebSocket message of message_type
and with a payload of buf_ref
and
chunked by fragment_size
. If the length of the buffer is less than the chunk size then
only a single message is sent.