Reconnect Slack RTM on connection error

This commit is contained in:
Jan Kaluza 2015-11-25 16:55:35 +01:00
parent 2225eb7765
commit fedd9216b2
2 changed files with 17 additions and 6 deletions

View file

@ -30,6 +30,7 @@
#include <Swiften/Network/Connection.h> #include <Swiften/Network/Connection.h>
#include <Swiften/Base/SafeByteArray.h> #include <Swiften/Base/SafeByteArray.h>
#include "Swiften/Version.h" #include "Swiften/Version.h"
#include "Swiften/Network/Timer.h"
#define HAVE_SWIFTEN_3 (SWIFTEN_VERSION >= 0x030000) #define HAVE_SWIFTEN_3 (SWIFTEN_VERSION >= 0x030000)
@ -64,6 +65,8 @@ class WebSocketClient {
void handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data); void handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data);
void handleConnected(bool error); void handleConnected(bool error);
void connectServer();
private: private:
Component *m_component; Component *m_component;
boost::shared_ptr<Swift::DomainNameAddressQuery> m_dnsQuery; boost::shared_ptr<Swift::DomainNameAddressQuery> m_dnsQuery;
@ -74,6 +77,7 @@ class WebSocketClient {
std::string m_path; std::string m_path;
std::string m_buffer; std::string m_buffer;
bool m_upgraded; bool m_upgraded;
Swift::Timer::ref m_reconnectTimer;
}; };
} }

View file

@ -45,6 +45,9 @@ WebSocketClient::WebSocketClient(Component *component) {
#else #else
m_tlsConnectionFactory = new Swift::TLSConnectionFactory(m_tlsFactory->getTLSContextFactory(), component->getNetworkFactories()->getConnectionFactory()); m_tlsConnectionFactory = new Swift::TLSConnectionFactory(m_tlsFactory->getTLSContextFactory(), component->getNetworkFactories()->getConnectionFactory());
#endif #endif
m_reconnectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1000);
m_reconnectTimer->onTick.connect(boost::bind(&WebSocketClient::connectServer, this));
} }
WebSocketClient::~WebSocketClient() { WebSocketClient::~WebSocketClient() {
@ -57,15 +60,18 @@ WebSocketClient::~WebSocketClient() {
delete m_tlsConnectionFactory; delete m_tlsConnectionFactory;
} }
void WebSocketClient::connectServer(const std::string &url) { void WebSocketClient::connectServer() {
std::string u = url.substr(6);
m_host = u.substr(0, u.find("/"));
m_path = u.substr(u.find("/"));
LOG4CXX_INFO(logger, "Starting DNS query for " << m_host << " " << m_path); LOG4CXX_INFO(logger, "Starting DNS query for " << m_host << " " << m_path);
m_dnsQuery = m_component->getNetworkFactories()->getDomainNameResolver()->createAddressQuery(m_host); m_dnsQuery = m_component->getNetworkFactories()->getDomainNameResolver()->createAddressQuery(m_host);
m_dnsQuery->onResult.connect(boost::bind(&WebSocketClient::handleDNSResult, this, _1, _2)); m_dnsQuery->onResult.connect(boost::bind(&WebSocketClient::handleDNSResult, this, _1, _2));
m_dnsQuery->run(); 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) { void WebSocketClient::write(const std::string &data) {
@ -165,7 +171,8 @@ void WebSocketClient::handleDataRead(boost::shared_ptr<Swift::SafeByteArray> dat
void WebSocketClient::handleConnected(bool error) { void WebSocketClient::handleConnected(bool error) {
if (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; return;
} }