From f57d03e4bb8bb52fb18d9ba77f194f2f07280da0 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Thu, 29 Apr 2021 19:14:23 -0700 Subject: [PATCH] WebSocketProtocol: Rename Process to ProcessMessage --- src/WebSocketProtocol.h | 3 ++- src/WebSocketServer.cpp | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/WebSocketProtocol.h b/src/WebSocketProtocol.h index ab021908..1825381d 100644 --- a/src/WebSocketProtocol.h +++ b/src/WebSocketProtocol.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "WebSocketServer.h" #include "WebSocketSession.h" @@ -13,5 +14,5 @@ namespace WebSocketProtocol { json result; }; - ProcessResult Process(websocketpp::connection_hdl hdl, WebSocketSession *session, json incomingMessage); + ProcessResult ProcessMessage(websocketpp::connection_hdl hdl, WebSocketSession *session, json incomingMessage); } \ No newline at end of file diff --git a/src/WebSocketServer.cpp b/src/WebSocketServer.cpp index 324a418c..0b9f3057 100644 --- a/src/WebSocketServer.cpp +++ b/src/WebSocketServer.cpp @@ -343,32 +343,30 @@ void WebSocketServer::onMessage(websocketpp::connection_hdl hdl, websocketpp::se uint8_t sessionEncoding = session.Encoding(); if (sessionEncoding == WebSocketEncoding::Json) { if (opcode != websocketpp::frame::opcode::text) { - if (!session.IgnoreInvalidMessages()) { + if (!session.IgnoreInvalidMessages()) _server.close(hdl, WebSocketCloseCode::MessageDecodeError, "The session encoding is set to Json, but the client sent a binary message.", errorCode); - } return; } + try { incomingMessage = json::parse(payload); } catch (json::parse_error& e) { - if (!session.IgnoreInvalidMessages()) { + if (!session.IgnoreInvalidMessages()) _server.close(hdl, WebSocketCloseCode::MessageDecodeError, std::string("Unable to decode Json: ") + e.what(), errorCode); - } return; } } else if (sessionEncoding == WebSocketEncoding::MsgPack) { if (opcode != websocketpp::frame::opcode::binary) { - if (!session.IgnoreInvalidMessages()) { + if (!session.IgnoreInvalidMessages()) _server.close(hdl, WebSocketCloseCode::MessageDecodeError, "The session encoding is set to MsgPack, but the client sent a text message.", errorCode); - } return; } + try { incomingMessage = json::from_msgpack(payload); } catch (json::parse_error& e) { - if (!session.IgnoreInvalidMessages()) { + if (!session.IgnoreInvalidMessages()) _server.close(hdl, WebSocketCloseCode::MessageDecodeError, std::string("Unable to decode MsgPack: ") + e.what(), errorCode); - } return; } } @@ -376,7 +374,7 @@ void WebSocketServer::onMessage(websocketpp::connection_hdl hdl, websocketpp::se if (_debugEnabled) blog(LOG_INFO, "[WebSocketServer::onMessage] Incoming message (decoded):\n%s", incomingMessage.dump(2).c_str()); - WebSocketProtocol::ProcessResult ret = WebSocketProtocol::Process(hdl, &session, incomingMessage); + WebSocketProtocol::ProcessResult ret = WebSocketProtocol::ProcessMessage(hdl, &session, incomingMessage); if (ret.closeCode) { websocketpp::lib::error_code errorCode; @@ -396,9 +394,11 @@ void WebSocketServer::onMessage(websocketpp::connection_hdl hdl, websocketpp::se } session.IncrementOutgoingMessages(); - if (errorCode) { + if (_debugEnabled) + blog(LOG_INFO, "[WebSocketServer::onMessage] Outgoing message:\n%s", ret.result.dump(2).c_str()); + + if (errorCode) blog(LOG_WARNING, "[WebSocketServer::onMessage] Sending message to client failed: %s", errorCode.message().c_str()); - } } }); }