mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Implementation for websocket heartbeat
This commit is contained in:
@@ -90,14 +90,10 @@ bool SignalServer::on_close(websocketpp::connection_hdl hdl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SignalServer::on_ping(websocketpp::connection_hdl hdl, std::string s) {
|
bool SignalServer::on_ping(websocketpp::connection_hdl hdl, std::string s) {
|
||||||
/* Do something */
|
|
||||||
LOG_INFO("Receive ping");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SignalServer::on_pong(websocketpp::connection_hdl hdl, std::string s) {
|
bool SignalServer::on_pong(websocketpp::connection_hdl hdl, std::string s) {
|
||||||
/* Do something */
|
|
||||||
LOG_INFO("pong");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,12 +29,16 @@ WsCore::~WsCore() {
|
|||||||
m_endpoint_.close(connection_handle_, websocketpp::close::status::going_away,
|
m_endpoint_.close(connection_handle_, websocketpp::close::status::going_away,
|
||||||
"", ec);
|
"", ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_INFO("> Error closing connection {}", ec.message());
|
LOG_INFO("Closing connection error: {}", ec.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_thread_->joinable()) {
|
if (m_thread_->joinable()) {
|
||||||
m_thread_->join();
|
m_thread_->join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ping_thread_->joinable()) {
|
||||||
|
ping_thread_->join();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int WsCore::Connect(std::string const &uri) {
|
int WsCore::Connect(std::string const &uri) {
|
||||||
@@ -45,7 +49,7 @@ int WsCore::Connect(std::string const &uri) {
|
|||||||
connection_handle_ = con->get_handle();
|
connection_handle_ = con->get_handle();
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_INFO("> Connect initialization error: {}", ec.message());
|
LOG_INFO("Connect initialization error: {}", ec.message());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,18 +61,15 @@ int WsCore::Connect(std::string const &uri) {
|
|||||||
websocketpp::lib::bind(&WsCore::OnClose, this, &m_endpoint_,
|
websocketpp::lib::bind(&WsCore::OnClose, this, &m_endpoint_,
|
||||||
websocketpp::lib::placeholders::_1));
|
websocketpp::lib::placeholders::_1));
|
||||||
|
|
||||||
// con->set_ping_handler(websocketpp::lib::bind(
|
con->set_ping_handler(websocketpp::lib::bind(
|
||||||
// &WsCore::on_ping,
|
&WsCore::OnPing, this, websocketpp::lib::placeholders::_1,
|
||||||
// this,
|
websocketpp::lib::placeholders::_2));
|
||||||
// websocketpp::lib::placeholders::_1,
|
|
||||||
// websocketpp::lib::placeholders::_2
|
|
||||||
// ));
|
|
||||||
|
|
||||||
con->set_pong_handler(websocketpp::lib::bind(
|
con->set_pong_handler(websocketpp::lib::bind(
|
||||||
&WsCore::OnPong, this, websocketpp::lib::placeholders::_1,
|
&WsCore::OnPong, this, websocketpp::lib::placeholders::_1,
|
||||||
websocketpp::lib::placeholders::_2));
|
websocketpp::lib::placeholders::_2));
|
||||||
|
|
||||||
con->set_pong_timeout(1000);
|
con->set_pong_timeout(10);
|
||||||
|
|
||||||
con->set_pong_timeout_handler(websocketpp::lib::bind(
|
con->set_pong_timeout_handler(websocketpp::lib::bind(
|
||||||
&WsCore::OnPongTimeout, this, websocketpp::lib::placeholders::_1,
|
&WsCore::OnPongTimeout, this, websocketpp::lib::placeholders::_1,
|
||||||
@@ -88,7 +89,7 @@ void WsCore::Close(websocketpp::close::status::value code, std::string reason) {
|
|||||||
|
|
||||||
m_endpoint_.close(connection_handle_, code, reason, ec);
|
m_endpoint_.close(connection_handle_, code, reason, ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_INFO("> Error initiating close: {}", ec.message());
|
LOG_INFO("Initiating close error: {}", ec.message());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,20 +99,22 @@ void WsCore::Send(std::string message) {
|
|||||||
m_endpoint_.send(connection_handle_, message,
|
m_endpoint_.send(connection_handle_, message,
|
||||||
websocketpp::frame::opcode::text, ec);
|
websocketpp::frame::opcode::text, ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_INFO("> Error sending message: {}", ec.message());
|
LOG_INFO("Sending message error: {}", ec.message());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WsCore::Ping() {
|
void WsCore::Ping(websocketpp::connection_hdl hdl) {
|
||||||
websocketpp::lib::error_code ec;
|
auto con = m_endpoint_.get_con_from_hdl(hdl);
|
||||||
|
while (con->get_state() == websocketpp::session::state::open) {
|
||||||
|
websocketpp::lib::error_code ec;
|
||||||
|
m_endpoint_.ping(hdl, "", ec);
|
||||||
|
if (ec) {
|
||||||
|
LOG_ERROR("Ping error: {}", ec.message());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::string message = "ping";
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||||
|
|
||||||
m_endpoint_.ping(connection_handle_, message, ec);
|
|
||||||
if (ec) {
|
|
||||||
LOG_INFO("> Error sending ping");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +122,9 @@ const std::string &WsCore::GetStatus() { return connection_status_; }
|
|||||||
|
|
||||||
void WsCore::OnOpen(client *c, websocketpp::connection_hdl hdl) {
|
void WsCore::OnOpen(client *c, websocketpp::connection_hdl hdl) {
|
||||||
connection_status_ = "Open";
|
connection_status_ = "Open";
|
||||||
|
|
||||||
|
ping_thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(
|
||||||
|
&WsCore::Ping, this, hdl);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) {
|
void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) {
|
||||||
@@ -129,9 +135,25 @@ void WsCore::OnClose(client *c, websocketpp::connection_hdl hdl) {
|
|||||||
connection_status_ = "Closed";
|
connection_status_ = "Closed";
|
||||||
}
|
}
|
||||||
|
|
||||||
void WsCore::OnPong(websocketpp::connection_hdl, std::string msg) {}
|
bool WsCore::OnPing(websocketpp::connection_hdl hdl, std::string msg) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void WsCore::OnPongTimeout(websocketpp::connection_hdl, std::string msg) {}
|
bool WsCore::OnPong(websocketpp::connection_hdl hdl, std::string msg) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WsCore::OnPongTimeout(websocketpp::connection_hdl hdl, std::string msg) {
|
||||||
|
if (timeout_count_ < 2) {
|
||||||
|
timeout_count_++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_WARN("Pong timeout, reset connection");
|
||||||
|
// m_endpoint_.close(hdl, websocketpp::close::status::normal,
|
||||||
|
// "OnPongTimeout");
|
||||||
|
m_endpoint_.reset();
|
||||||
|
}
|
||||||
|
|
||||||
void WsCore::OnMessage(websocketpp::connection_hdl, client::message_ptr msg) {
|
void WsCore::OnMessage(websocketpp::connection_hdl, client::message_ptr msg) {
|
||||||
OnReceiveMessage(msg->get_payload());
|
OnReceiveMessage(msg->get_payload());
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "websocketpp/client.hpp"
|
#include "websocketpp/client.hpp"
|
||||||
#include "websocketpp/common/memory.hpp"
|
#include "websocketpp/common/memory.hpp"
|
||||||
@@ -24,7 +25,7 @@ class WsCore {
|
|||||||
|
|
||||||
void Send(std::string message);
|
void Send(std::string message);
|
||||||
|
|
||||||
void Ping();
|
void Ping(websocketpp::connection_hdl hdl);
|
||||||
|
|
||||||
const std::string &GetStatus();
|
const std::string &GetStatus();
|
||||||
|
|
||||||
@@ -35,11 +36,13 @@ class WsCore {
|
|||||||
|
|
||||||
void OnClose(client *c, websocketpp::connection_hdl hdl);
|
void OnClose(client *c, websocketpp::connection_hdl hdl);
|
||||||
|
|
||||||
void OnPong(websocketpp::connection_hdl, std::string msg);
|
bool OnPing(websocketpp::connection_hdl hdl, std::string msg);
|
||||||
|
|
||||||
void OnPongTimeout(websocketpp::connection_hdl, std::string msg);
|
bool OnPong(websocketpp::connection_hdl hdl, std::string msg);
|
||||||
|
|
||||||
void OnMessage(websocketpp::connection_hdl, client::message_ptr msg);
|
void OnPongTimeout(websocketpp::connection_hdl hdl, std::string msg);
|
||||||
|
|
||||||
|
void OnMessage(websocketpp::connection_hdl hdl, client::message_ptr msg);
|
||||||
|
|
||||||
virtual void OnReceiveMessage(const std::string &msg) = 0;
|
virtual void OnReceiveMessage(const std::string &msg) = 0;
|
||||||
|
|
||||||
@@ -47,8 +50,10 @@ class WsCore {
|
|||||||
client m_endpoint_;
|
client m_endpoint_;
|
||||||
websocketpp::connection_hdl connection_handle_;
|
websocketpp::connection_hdl connection_handle_;
|
||||||
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread_;
|
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread_;
|
||||||
|
websocketpp::lib::shared_ptr<websocketpp::lib::thread> ping_thread_;
|
||||||
|
|
||||||
std::string connection_status_ = "Connecting";
|
std::string connection_status_ = "Connecting";
|
||||||
|
int timeout_count_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user