diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 2c9d50b8..fc4f88b4 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -229,13 +229,9 @@ namespace ix using millis = std::chrono::duration; millis duration; - while (true) + // Try to connect only once when we don't have automaticReconnection setup + if (!isConnected() && !_automaticReconnection && !_stop) { - if (isConnected() || isClosing() || _stop || !_automaticReconnection) - { - break; - } - status = connect(_handshakeTimeoutSecs); if (!status.success && !_stop) @@ -249,8 +245,33 @@ namespace ix _onMessageCallback(WebSocket_MessageType_Error, "", 0, connectErr, WebSocketOpenInfo(), WebSocketCloseInfo()); + } + } + else + { + while (true) + { + if (isConnected() || isClosing() || _stop || !_automaticReconnection) + { + break; + } - std::this_thread::sleep_for(duration); + status = connect(_handshakeTimeoutSecs); + + if (!status.success && !_stop) + { + duration = millis(calculateRetryWaitMilliseconds(retries++)); + + connectErr.retries = retries; + connectErr.wait_time = duration.count(); + connectErr.reason = status.errorStr; + connectErr.http_status = status.http_status; + _onMessageCallback(WebSocket_MessageType_Error, "", 0, + connectErr, WebSocketOpenInfo(), + WebSocketCloseInfo()); + + std::this_thread::sleep_for(duration); + } } } } @@ -317,7 +338,7 @@ namespace ix // 4. In blocking mode, getting out of this function is triggered by // an explicit disconnection from the callback, or by the remote end // closing the connection, ie isConnected() == false. - if (!_thread.joinable() && !isConnected() && !_automaticReconnection) return; + if (!isConnected() && !_automaticReconnection) return; } } diff --git a/ws/ws_connect.cpp b/ws/ws_connect.cpp index 5794d0da..55332eb0 100644 --- a/ws/ws_connect.cpp +++ b/ws/ws_connect.cpp @@ -39,6 +39,7 @@ namespace ix std::cout << "Disabling automatic reconnection with " "_webSocket.disableAutomaticReconnection()" " not supported yet" << std::endl; + _webSocket.disableAutomaticReconnection(); } }