From fedd9216b2c950d8773432845d390518ce88b052 2015-11-25 16:55:35 From: Jan Kaluza Date: 2015-11-25 16:55:35 Subject: [PATCH] Reconnect Slack RTM on connection error --- diff --git a/include/transport/WebSocketClient.h b/include/transport/WebSocketClient.h index 878025f68ea501899667d70065f88811815737e4..d936e0d0d00fc2d6e5641f9e0183e91933fffad8 100644 --- a/include/transport/WebSocketClient.h +++ b/include/transport/WebSocketClient.h @@ -30,6 +30,7 @@ #include #include #include "Swiften/Version.h" +#include "Swiften/Network/Timer.h" #define HAVE_SWIFTEN_3 (SWIFTEN_VERSION >= 0x030000) @@ -64,6 +65,8 @@ class WebSocketClient { void handleDataRead(boost::shared_ptr data); void handleConnected(bool error); + void connectServer(); + private: Component *m_component; boost::shared_ptr m_dnsQuery; @@ -74,6 +77,7 @@ class WebSocketClient { std::string m_path; std::string m_buffer; bool m_upgraded; + Swift::Timer::ref m_reconnectTimer; }; } diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index b18b5da85ca6562798b14b84415180e20249bdf5..53cc6849c01ad831417ea32aaaf6cfbf35e67a8f 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -45,6 +45,9 @@ WebSocketClient::WebSocketClient(Component *component) { #else m_tlsConnectionFactory = new Swift::TLSConnectionFactory(m_tlsFactory->getTLSContextFactory(), component->getNetworkFactories()->getConnectionFactory()); #endif + + m_reconnectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1000); + m_reconnectTimer->onTick.connect(boost::bind(&WebSocketClient::connectServer, this)); } WebSocketClient::~WebSocketClient() { @@ -57,15 +60,18 @@ WebSocketClient::~WebSocketClient() { delete m_tlsConnectionFactory; } -void WebSocketClient::connectServer(const std::string &url) { - std::string u = url.substr(6); - m_host = u.substr(0, u.find("/")); - m_path = u.substr(u.find("/")); - +void WebSocketClient::connectServer() { LOG4CXX_INFO(logger, "Starting DNS query for " << m_host << " " << m_path); m_dnsQuery = m_component->getNetworkFactories()->getDomainNameResolver()->createAddressQuery(m_host); m_dnsQuery->onResult.connect(boost::bind(&WebSocketClient::handleDNSResult, this, _1, _2)); m_dnsQuery->run(); + m_reconnectTimer->stop(); +} + +void WebSocketClient::connectServer(const std::string &url) { + std::string u = url.substr(6); + m_host = u.substr(0, u.find("/")); + m_path = u.substr(u.find("/")); } void WebSocketClient::write(const std::string &data) { @@ -165,7 +171,8 @@ void WebSocketClient::handleDataRead(boost::shared_ptr dat void WebSocketClient::handleConnected(bool error) { if (error) { - LOG4CXX_ERROR(logger, "Connection to " << m_host << " failed"); + LOG4CXX_ERROR(logger, "Connection to " << m_host << " failed. Will reconnect in 1 second."); + m_reconnectTimer->start(); return; }