pub struct MessageCodec<M>
where M: Message,
{ /* private fields */ }
Expand description

A codec for asynchronously decoding and encoding websocket messages.

This codec decodes messages into the OwnedMessage struct, so using this the user will receive messages as OwnedMessages. However it can encode any type of message that implements the ws::Message trait (that type is decided by the M type parameter) like OwnedMessage and Message.

Warning: if you don’t know what your doing or want a simple websocket connection please use the ClientBuilder or the Server structs. You should only use this after a websocket handshake has already been completed on the stream you are using.

§Example (for the high-level websocket crate)

use websocket::async::{MessageCodec, MsgCodecCtx};

let mut runtime = tokio::runtime::Builder::new().build().unwrap();
let mut input = Vec::new();
Message::text("50 schmeckels").serialize(&mut input, false);

let f = MessageCodec::default(MsgCodecCtx::Client)
    .framed(ReadWritePair(Cursor::new(input), Cursor::new(vec![])))
    .into_future()
    .map_err(|e| e.0)
    .map(|(m, _)| {
        assert_eq!(m, Some(OwnedMessage::Text("50 schmeckels".to_string())));
    });

runtime.block_on(f).unwrap();

Implementations§

source§

impl MessageCodec<OwnedMessage>

source

pub fn default(context: Context) -> MessageCodec<OwnedMessage>

Create a new MessageCodec with a role of context (either Client or Server) to read and write messages asynchronously.

This will create the crate’s default codec which sends and receives OwnedMessage structs. The message data has to be sent to an intermediate buffer anyway so sending owned data is preferable.

If you have your own implementation of websocket messages, you can use the new method to create a codec for that implementation.

source§

impl<M> MessageCodec<M>
where M: Message,

source

pub fn new(context: Context) -> MessageCodec<M>

Creates a codec that can encode a custom implementation of a websocket message.

If you just want to use a normal codec without a specific implementation of a websocket message, take a look at MessageCodec::default.

The codec automatically imposes default limits on message and data frame size. Use new_with_limits to override them.

source

pub fn new_with_limits( context: Context, max_dataframe_size: usize, max_message_size: usize ) -> MessageCodec<M>

Trait Implementations§

source§

impl<M> Decoder for MessageCodec<M>
where M: Message,

§

type Item = OwnedMessage

The type of decoded frames.
§

type Error = WebSocketError

The type of unrecoverable frame decoding errors. Read more
source§

fn decode( &mut self, src: &mut BytesMut ) -> Result<Option<<MessageCodec<M> as Decoder>::Item>, <MessageCodec<M> as Decoder>::Error>

Attempts to decode a frame from the provided buffer of bytes. Read more
source§

fn decode_eof( &mut self, buf: &mut BytesMut ) -> Result<Option<Self::Item>, Self::Error>

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
source§

fn framed<T>(self, io: T) -> Framed<T, Self>
where T: AsyncRead + AsyncWrite, Self: Encoder + Sized,

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more
source§

impl<M> Encoder for MessageCodec<M>
where M: Message,

§

type Item = M

The type of items consumed by the Encoder
§

type Error = WebSocketError

The type of encoding errors. Read more
source§

fn encode( &mut self, item: <MessageCodec<M> as Encoder>::Item, dst: &mut BytesMut ) -> Result<(), <MessageCodec<M> as Encoder>::Error>

Encodes a frame into the buffer provided. Read more

Auto Trait Implementations§

§

impl<M> Freeze for MessageCodec<M>

§

impl<M> RefUnwindSafe for MessageCodec<M>

§

impl<M> Send for MessageCodec<M>

§

impl<M> Sync for MessageCodec<M>

§

impl<M> Unpin for MessageCodec<M>

§

impl<M> UnwindSafe for MessageCodec<M>

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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.