Changeset - fedd9216b2c9
[Not reviewed]
0 2 0
Jan Kaluza - 10 years ago 2015-11-25 16:55:35
jkaluza@redhat.com
Reconnect Slack RTM on connection error
2 files changed with 17 insertions and 6 deletions:
0 comments (0 inline, 0 general)
include/transport/WebSocketClient.h
Show inline comments
 
@@ -30,6 +30,7 @@
 
#include <Swiften/Network/Connection.h>
 
#include <Swiften/Base/SafeByteArray.h>
 
#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<Swift::SafeByteArray> data);
 
		void handleConnected(bool error);
 

	
 
		void connectServer();
 

	
 
	private:
 
		Component *m_component;
 
		boost::shared_ptr<Swift::DomainNameAddressQuery> m_dnsQuery;
 
@@ -74,6 +77,7 @@ class WebSocketClient {
 
		std::string m_path;
 
		std::string m_buffer;
 
		bool m_upgraded;
 
		Swift::Timer::ref m_reconnectTimer;
 
};
 

	
 
}
src/WebSocketClient.cpp
Show inline comments
 
@@ -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<Swift::SafeByteArray> 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;
 
	}
 

	
0 comments (0 inline, 0 general)