title: Socket reconnect logic
body: <p>I'm using the following websocket library for Android:</p>
  
  <p><a href="https://github.com/TakahikoKawasaki/nv-websocket-client" rel="nofollow noreferrer">https://github.com/TakahikoKawasaki/nv-websocket-client</a></p>
  
  <p>I want to be able to reconnect every 5 seconds on disconnect as long as the current activity is  alive. This is my logic.</p>
  
  <pre><code>    @Override
      public void onConnected(WebSocket websocket, Map&lt;String, List&lt;String&gt;&gt; headers) throws Exception {
          Log.e(TAG, "connected");
          mIsActivityAlive = true;
          super.onConnected(websocket, headers);
      }
  
      @Override
      public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
          super.onDisconnected(websocket, serverCloseFrame, clientCloseFrame, closedByServer);
          Log.e(TAG, "disconnected");
          if (mIsActivityAlive) {
              while(!mWs.isOpen()) {
                  Thread.sleep(5 * 1000);
                  Log.e(TAG, "reconnecting");
                  connectWs();
              }
          }
      }
  
  public void connectWs() {
      try {
          mWs = new WebSocketFactory().setConnectionTimeout(5 * 1000).createSocket("MY_WS_URL");
          mWs.addListener(wsAdapter);
          mWs.connectAsynchronously();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This logic works fine - however, after the websocket connects after reconnect, it keeps trying to reconnect. I thought having <code>!mWs.isOpen</code> in the while loop would solve this issue. I logged <code>mWs.isOpen</code> in the <code>onConnected</code> callback and it's <code>true</code> , however it keeps appearing <code>false</code> in the <code>onDisconnected</code> callback. I have a feeling it has to do with the <code>Thread.sleep</code> but I'm not sure where to start looking.</p>