From f592dc0ef3a63dae4775cb444deea1788625a6c9 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Mon, 28 Dec 2015 09:17:41 +0100 Subject: [PATCH] Reconnect to Slack RTM when disconnected with an error --- include/transport/WebSocketClient.h | 1 + libtransport/WebSocketClient.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/transport/WebSocketClient.h b/include/transport/WebSocketClient.h index c90638b2..c078900e 100644 --- a/include/transport/WebSocketClient.h +++ b/include/transport/WebSocketClient.h @@ -66,6 +66,7 @@ class WebSocketClient { void handleDNSResult(const std::vector&, boost::optional); void handleDataRead(boost::shared_ptr data); void handleConnected(bool error); + void handleDisconnected(const boost::optional &error); void connectServer(); diff --git a/libtransport/WebSocketClient.cpp b/libtransport/WebSocketClient.cpp index 74f399d8..dc063fe0 100644 --- a/libtransport/WebSocketClient.cpp +++ b/libtransport/WebSocketClient.cpp @@ -208,10 +208,20 @@ void WebSocketClient::handleConnected(bool error) { m_conn->write(Swift::createSafeByteArray(req)); } +void WebSocketClient::handleDisconnected(const boost::optional &error) { + if (!error) { + return; + } + + LOG4CXX_ERROR(logger, "Disconected from " << m_host << ". Will reconnect in 1 second."); + m_reconnectTimer->start(); +} + void WebSocketClient::handleDNSResult(const std::vector &addrs, boost::optional) { m_conn = m_tlsConnectionFactory->createConnection(); m_conn->onDataRead.connect(boost::bind(&WebSocketClient::handleDataRead, this, _1)); m_conn->onConnectFinished.connect(boost::bind(&WebSocketClient::handleConnected, this, _1)); + m_conn->onDisconnected.connect(boost::bind(&WebSocketClient::handleDisconnected, this, _1)); m_conn->connect(Swift::HostAddressPort(addrs[0], 443)); }