[][src]Struct websocket::codec::ws::MessageCodec

pub struct MessageCodec<M> where
    M: MessageTrait
{ /* fields omitted */ }

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

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();

Methods

impl MessageCodec<OwnedMessage>
[src]

pub fn default(context: Context) -> Self
[src]

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.

impl<M> MessageCodec<M> where
    M: MessageTrait
[src]

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

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.

Trait Implementations

impl<M> Encoder for MessageCodec<M> where
    M: MessageTrait
[src]

type Item = M

The type of items consumed by the Encoder

type Error = WebSocketError

The type of encoding errors. Read more

impl<M> Decoder for MessageCodec<M> where
    M: MessageTrait
[src]

type Item = OwnedMessage

The type of decoded frames.

type Error = WebSocketError

The type of unrecoverable frame decoding errors. Read more

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

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

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

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

Auto Trait Implementations

impl<M> Send for MessageCodec<M>

impl<M> Sync for MessageCodec<M>

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Typeable for T where
    T: Any

fn get_type(&self) -> TypeId

Get the TypeId of this object.

impl<T> Erased for T