Struct tokio_binance::WebSocketStream[][src]

pub struct WebSocketStream { /* fields omitted */ }

Websocket stream for the various binance channels aka streams.

Implementations

impl WebSocketStream[src]

pub async fn connect<U: Into<String>>(
    channel: Channel<'_>,
    url: U
) -> Result<Self>
[src]

Start websocket stream by connecting to a channel.

Example

use tokio_binance::{WebSocketStream, BINANCE_US_WSS_URL, Channel};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let channel = Channel::Ticker("BNBUSDT");
    let mut stream = WebSocketStream::connect(channel, BINANCE_US_WSS_URL).await?;
    Ok(())
}

pub async fn text(&mut self) -> Result<Option<String>>[src]

Helper method for getting messages as text.

Example

while let Some(text) = stream.text().await? {
    println!("{}", text);
}

pub async fn json<J: DeserializeOwned>(&mut self) -> Result<Option<J>>[src]

Helper method for getting messages as a serde deserializable.

Example

use serde_json::Value;

while let Some(value) = stream.json::<Value>().await? {
    // filter the messages before accessing a field.
    if channel == value["stream"] {
        println!("{}", serde_json::to_string_pretty(&value)?);
    }
}

pub async fn subscribe(&mut self, channels: &[Channel<'_>]) -> Result<()>[src]

Subscribe to one or more channels aka streams.

Example

use tokio_binance::{Channel, Interval};

stream.subscribe(&[
    Channel::AggTrade("BNBUSDT"),
    Channel::Ticker("BTCUSDT"),
    Channel::Kline("BNBUSDT", Interval::OneMinute)
    // and so on
]).await?;

pub async fn unsubscribe(&mut self, channels: &[Channel<'_>]) -> Result<()>[src]

Unsubscribe from one or more channels aka streams.

Example

use tokio_binance::{Channel, Interval};

stream.unsubscribe(&[
    Channel::AggTrade("BNBUSDT"),
    Channel::Kline("BNBUSDT", Interval::OneMinute)
    // and so on
]).await?;

pub fn get_ref(
    &self
) -> &(WsStream<StreamSwitcher<TokioAdapter<TcpStream>, TokioAdapter<TlsStream<TcpStream>>>>, Response)
[src]

Returns a shared reference to the inner stream.

pub fn get_mut(
    &mut self
) -> &mut (WsStream<StreamSwitcher<TokioAdapter<TcpStream>, TokioAdapter<TlsStream<TcpStream>>>>, Response)
[src]

Returns a mutable reference to the inner stream.

pub async fn close(&mut self, msg: Option<CloseFrame<'_>>) -> Result<()>[src]

Close the underlying web socket

Trait Implementations

impl Sink<Message> for WebSocketStream[src]

type Error = Error

The type of value produced by the sink when an error occurs.

impl Stream for WebSocketStream[src]

type Item = Result<Message>

Values yielded by the stream.

Auto Trait Implementations

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

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

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, Item> SinkExt<Item> for T where
    T: Sink<Item> + ?Sized

impl<T> StreamExt for T where
    T: Stream + ?Sized

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

impl<S> TryStreamExt for S where
    S: TryStream + ?Sized

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,