Rust Websocket disconnects, ok to just reconnect?

Hi,
First of all, thank you to the Binance team for such a nice API.
I connect using Rust and tungstenite WebSocket to the following url:
wss://stream.binance.com:9443/stream?streams=ethbtc@depth5@100ms/bnbeth@depth5@100ms

but after 30min - 60min or so, I get disconnected. Is that normal? And is it ok to just try a reconnect or will that get me banned?

I read a message from the socket every 100ms.

If I listen to 13 streams @100ms I am getting disconnected even faster and only 1 ping frame seems to get through to my connection.

Here are some of the errors I am getting:

2021-09-03 14:16:35 - TRACE: Opcode: Data(Text)
2021-09-03 14:16:35 - TRACE: Masked: false
2021-09-03 14:16:35 - TRACE: no frame received
2021-09-03 14:16:35 - ERROR: Error reading message: Protocol(ResetWithoutClosingHandshake)

OR

2021-09-03 07:01:00 - DEBUG: Received close frame: Some(CloseFrame { code: Policy, reason: “Pong timeout” })
2021-09-03 07:01:00 - DEBUG: Replying to close with Frame { header: FrameHeader { is_final: true, rsv1: false, rsv2: false, rsv3: false, opcode: Control(Close), mask: None }, payload: [3, 232] }
2021-09-03 07:01:00 - ERROR: Error getting text: Close(Some(CloseFrame { code: Policy, reason: “Pong timeout” }))

OR (the one subscribed to 13 streams)

2021-09-02 20:57:00 - ERROR: Error reading message: Io(Os { code: 54, kind: ConnectionReset, message: “Connection reset by peer” })
2021-09-02 20:57:00 - ERROR: Error reading message: Protocol(ResetWithoutClosingHandshake)

More details:

Receiving ping and sending pong is automatically handled by the library I am using (tungstenite)
2021-09-03 13:46:02 - TRACE: Opcode: Control(Ping)
2021-09-03 13:46:02 - TRACE: Masked: false
2021-09-03 13:46:02 - TRACE: received frame

final: true reserved: false false false opcode: PING length: 15 payload length: 13 payload: 0x31363330363736363930393039

2021-09-03 13:46:02 - TRACE: Received message 1630676690909
2021-09-03 13:46:02 - TRACE: Sending pong reply
2021-09-03 13:46:02 - TRACE: Sending frame: Frame { header: FrameHeader { is_final: true, rsv1: false, rsv2: false, rsv3: false, opcode: Control(Pong), mask: Some([171, 171, 8, 38]) }, payload: [49, 54, 51, 48, 54, 55, 54, 54, 57, 48, 57, 48, 57] }
2021-09-03 13:46:02 - TRACE: writing frame

final: true reserved: false false false opcode: PONG length: 19 payload length: 13 payload: 0x31363330363736363930393039

2021-09-03 13:46:02 - TRACE: Frames still in queue: 0
2021-09-03 13:46:02 - TRACE: Parsed headers [129, 126]
2021-09-03 13:46:02 - TRACE: First: 10000001
2021-09-03 13:46:02 - TRACE: Second: 1111110

If you could advise me if reconnecting is appropriate or not or a different solution, I would appreciate it thanks.

Code for anyone interested:

use log::{debug, error, info};
use tokio::{sync::broadcast::error, time::Duration, time::Instant};
use tungstenite::connect;
use tungstenite::{client::AutoStream, error::Error, WebSocket};
use url::Url;

mod models;

static BINANCE_WS_API: &str = "wss://stream.binance.com:9443";
#[tokio::main]
async fn main() {
   log4rs::init_file("log_config.yaml", Default::default()).unwrap();
   info!("starting...");
   let binance_url = format!(
       "{}/stream?streams=ethbtc@depth5@100ms/bnbeth@depth5@100ms",
       BINANCE_WS_API
   );
   let (mut socket, response) =
       connect(Url::parse(&binance_url).unwrap()).expect("Can't connect.");

   info!("Connected to binance stream.");
   debug!("HTTP status code: {}", response.status());
   debug!("Response headers:");
   for (ref header, ref header_value) in response.headers() {
       debug!("- {}: {:?}", header, header_value);
   }

   loop {
       tokio::time::sleep(Duration::from_millis(100)).await;
       let msg = match socket.read_message() {
           Ok(m) => m,
           Err(err) => {
               error!("Error reading message: {:?}", err);
               continue;
           }
       };

       let msg = match msg {
           tungstenite::Message::Text(s) => s
           _ => {
               error!("Error getting text: {:?}", msg);
               continue;
           }
       };

       let parsed: models::DepthStreamWrapper = serde_json::from_str(&msg).expect("Can't parse");
       info!(
           "{}: {}. ask: {}, size: {}",
           parsed.stream, 0, parsed.data.asks[0].price, parsed.data.asks[0].size
       );
   }
}

can you keep the connection longer for just one stream?

Hi, thanks for your reply.
I did some more experimenting and found that the sleep I am using in the loop was the culprit. I wasn’t reading the messages fast enough and pings got dropped or something like that.

So, the solution is don’t put in a sleep when reading messages from a socket, haha.