Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add subscription from slice of Streams #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/tungstenite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,49 @@ impl<T: Read + Write> WebSocketState<T> {
self.send("SUBSCRIBE", streams.into_iter().map(|s| s.as_str()))
}

/// Sends `SUBSCRIBE` message for the given `streams` slice.
///
/// `streams` are not validated. Invalid streams will be
/// accepted by the server, but no data will be sent.
/// Requests to subscribe an existing stream will be ignored
/// by the server.
///
/// Returns the message `id`. This should be used to match
/// the request with a future response. Sent messages should
/// not share the same message `id`.
///
/// You should expect the server to respond with a similar
/// message.
/// ```json
/// { "method": "SUBSCRIBE", "params": [ <streams> ], "id": <id> }
/// ```
///
/// # Example
/// ```rust
/// use binance_spot_connector_rust::{
/// market_stream::book_ticker::BookTickerStream,
/// tungstenite::BinanceWebSocketClient,
/// };
/// let stream_adr = "wss://stream.binance.com:9443/ws";
/// let mut conn = BinanceWebSocketClient::connect_with_url(stream_adr).unwrap();
///
/// let symbol_lst = vec!["BTCUSDT", "BNBBUSD"];
/// let lst = symbol_lst.iter()
/// .map(|x| BookTickerStream::from_symbol(&x).into())
/// .collect::<Vec<_>>()
/// ;
/// conn.subscribe_from_slice(&lst);
/// while let Ok(message) = conn.as_mut().read() {
/// let data = message.into_data();
/// let string_data = unsafe { String::from_utf8_unchecked(data) };
/// assert!(string_data.len() > 0);
/// }
/// ```
pub fn subscribe_from_slice(&mut self, streams: &[Stream]) -> u64
{
self.send("SUBSCRIBE", streams.iter().map(|s| s.as_str()))
}

/// Sends `UNSUBSCRIBE` message for the given `streams`.
///
/// `streams` are not validated. Non-existing streams will be
Expand Down