From 84021e06dcf1d29975ea68fe7922b374271263e3 Mon Sep 17 00:00:00 2001 From: Yibai Zhang Date: Tue, 9 Jan 2024 22:17:32 +0800 Subject: [PATCH] fix(QWebsocketpp): add more exception handling Signed-off-by: Yibai Zhang --- src/util/QWebsocketpp/QWebsocketpp.cpp | 39 ++++++++++++++++---------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/util/QWebsocketpp/QWebsocketpp.cpp b/src/util/QWebsocketpp/QWebsocketpp.cpp index c1efea7..9c5b492 100644 --- a/src/util/QWebsocketpp/QWebsocketpp.cpp +++ b/src/util/QWebsocketpp/QWebsocketpp.cpp @@ -249,15 +249,19 @@ qint64 QWebsocketpp::sendBinaryMessage(const QByteArray &data) if (!running) { return 0; } - - if (client_is_tls) { - wss_client->send(hdl, data.toStdString(), - websocketpp::frame::opcode::binary); - } else { - ws_client->send(hdl, data.toStdString(), - websocketpp::frame::opcode::binary); + try { + if (client_is_tls) { + wss_client->send(hdl, data.toStdString(), + websocketpp::frame::opcode::binary); + } else { + ws_client->send(hdl, data.toStdString(), + websocketpp::frame::opcode::binary); + } + return data.size(); + } catch (websocketpp::exception const &e) { + std::cout << e.what(); + return 0; } - return data.size(); } qint64 QWebsocketpp::sendTextMessage(const QString &message) @@ -266,14 +270,19 @@ qint64 QWebsocketpp::sendTextMessage(const QString &message) if (!running) { return 0; } - if (client_is_tls) { - wss_client->send(hdl, message.toStdString(), - websocketpp::frame::opcode::text); - } else { - ws_client->send(hdl, message.toStdString(), - websocketpp::frame::opcode::text); + try { + if (client_is_tls) { + wss_client->send(hdl, message.toStdString(), + websocketpp::frame::opcode::text); + } else { + ws_client->send(hdl, message.toStdString(), + websocketpp::frame::opcode::text); + } + return message.size(); + } catch (websocketpp::exception const &e) { + std::cout << e.what(); + return 0; } - return message.size(); } QString QWebsocketpp::errorString() const