Compare commits
4 Commits
v5.1.2
...
feature/no
Author | SHA1 | Date | |
---|---|---|---|
e02679f744 | |||
21c155339e | |||
bbf34aef29 | |||
225aade89d |
@ -38,7 +38,6 @@ namespace ix
|
|||||||
WebSocket::WebSocket() :
|
WebSocket::WebSocket() :
|
||||||
_onMessageCallback(OnMessageCallback()),
|
_onMessageCallback(OnMessageCallback()),
|
||||||
_stop(false),
|
_stop(false),
|
||||||
_backgroundThreadRunning(false),
|
|
||||||
_automaticReconnection(true),
|
_automaticReconnection(true),
|
||||||
_handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs),
|
_handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs),
|
||||||
_enablePong(kDefaultEnablePong),
|
_enablePong(kDefaultEnablePong),
|
||||||
@ -136,7 +135,6 @@ namespace ix
|
|||||||
{
|
{
|
||||||
if (_thread.joinable()) return; // we've already been started
|
if (_thread.joinable()) return; // we've already been started
|
||||||
|
|
||||||
_backgroundThreadRunning = true;
|
|
||||||
_thread = std::thread(&WebSocket::run, this);
|
_thread = std::thread(&WebSocket::run, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,7 +155,6 @@ namespace ix
|
|||||||
|
|
||||||
_stop = true;
|
_stop = true;
|
||||||
_thread.join();
|
_thread.join();
|
||||||
_backgroundThreadRunning = false;
|
|
||||||
_stop = false;
|
_stop = false;
|
||||||
|
|
||||||
_automaticReconnection = automaticReconnection;
|
_automaticReconnection = automaticReconnection;
|
||||||
@ -232,16 +229,12 @@ namespace ix
|
|||||||
using millis = std::chrono::duration<double, std::milli>;
|
using millis = std::chrono::duration<double, std::milli>;
|
||||||
millis duration;
|
millis duration;
|
||||||
|
|
||||||
while (true)
|
// Try to connect only once when we don't have automaticReconnection setup
|
||||||
|
if (!isConnected() && !isClosing() && !_stop && !_automaticReconnection)
|
||||||
{
|
{
|
||||||
if (isConnected() || isClosing() || _stop || !_automaticReconnection)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
status = connect(_handshakeTimeoutSecs);
|
status = connect(_handshakeTimeoutSecs);
|
||||||
|
|
||||||
if (!status.success && !_stop)
|
if (!status.success)
|
||||||
{
|
{
|
||||||
duration = millis(calculateRetryWaitMilliseconds(retries++));
|
duration = millis(calculateRetryWaitMilliseconds(retries++));
|
||||||
|
|
||||||
@ -252,8 +245,38 @@ namespace ix
|
|||||||
_onMessageCallback(WebSocket_MessageType_Error, "", 0,
|
_onMessageCallback(WebSocket_MessageType_Error, "", 0,
|
||||||
connectErr, WebSocketOpenInfo(),
|
connectErr, WebSocketOpenInfo(),
|
||||||
WebSocketCloseInfo());
|
WebSocketCloseInfo());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Otherwise try to reconnect perpertually
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (isConnected() || isClosing() || _stop || !_automaticReconnection)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::this_thread::sleep_for(duration);
|
status = connect(_handshakeTimeoutSecs);
|
||||||
|
|
||||||
|
if (!status.success)
|
||||||
|
{
|
||||||
|
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());
|
||||||
|
|
||||||
|
// Only sleep if we aren't in the middle of stopping
|
||||||
|
if (!_stop)
|
||||||
|
{
|
||||||
|
std::this_thread::sleep_for(duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -320,8 +343,7 @@ namespace ix
|
|||||||
// 4. In blocking mode, getting out of this function is triggered by
|
// 4. In blocking mode, getting out of this function is triggered by
|
||||||
// an explicit disconnection from the callback, or by the remote end
|
// an explicit disconnection from the callback, or by the remote end
|
||||||
// closing the connection, ie isConnected() == false.
|
// closing the connection, ie isConnected() == false.
|
||||||
// closing the connection, ie isConnectedOrClosing() == false.
|
if (!isConnected() && !_automaticReconnection) return;
|
||||||
if (!_backgroundThreadRunning && !isConnected() && !_automaticReconnection) return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +39,7 @@ namespace ix
|
|||||||
std::cout << "Disabling automatic reconnection with "
|
std::cout << "Disabling automatic reconnection with "
|
||||||
"_webSocket.disableAutomaticReconnection()"
|
"_webSocket.disableAutomaticReconnection()"
|
||||||
" not supported yet" << std::endl;
|
" not supported yet" << std::endl;
|
||||||
|
_webSocket.disableAutomaticReconnection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user