55#include < utils/JsonUtils.h>
66#include < utils/NetOrigin.h>
77
8+ #define NO_TRACE_SEND
9+ #define NO_TRACE_RECEIVE
10+
811WebSocketJsonHandler::WebSocketJsonHandler (QWebSocket* websocket, QObject* parent)
912 : QObject(parent)
1013 , _websocket(websocket)
1114 , _log(Logger::getInstance(" WEBSOCKET" ))
1215{
1316 connect (_websocket, &QWebSocket::textMessageReceived, this , &WebSocketJsonHandler::onTextMessageReceived);
17+ connect (_websocket, &QWebSocket::binaryMessageReceived, this , &WebSocketJsonHandler::onBinaryMessageReceived);
1418 connect (_websocket, &QWebSocket::disconnected, this , &WebSocketJsonHandler::onDisconnected);
1519
16- const QString client = _websocket->peerAddress ().toString ();
17- Debug (_log, " New WebSocket connection from %s" , QSTRING_CSTR (client));
20+ _peerAddress = _websocket->peerAddress ().toString ();
21+ _origin = websocket->origin ();
22+ Debug (_log, " New WebSocket connection from %s initiated via: %s" , QSTRING_CSTR (_peerAddress), QSTRING_CSTR (_origin));
1823
1924 bool localConnection = NetOrigin::getInstance ()->isLocalAddress (_websocket->peerAddress (), _websocket->localAddress ());
2025
2126 // Json processor
22- _jsonAPI.reset (new JsonAPI (client , _log, localConnection, this ));
27+ _jsonAPI.reset (new JsonAPI (_peerAddress , _log, localConnection, this ));
2328
2429 connect (_jsonAPI.get (), &JsonAPI::callbackReady, this , &WebSocketJsonHandler::sendMessage);
2530 connect (_jsonAPI->getCallBack ().get (), &JsonCallbacks::callbackReady, this , &WebSocketJsonHandler::sendMessage);
@@ -30,20 +35,29 @@ WebSocketJsonHandler::WebSocketJsonHandler(QWebSocket* websocket, QObject* paren
3035
3136void WebSocketJsonHandler::onTextMessageReceived (const QString& message)
3237{
33- qDebug () << " WebSocket message received:" << message;
34- _jsonAPI.get ()->handleMessage (message);
38+ #ifdef RECEIVE_TRACE
39+ qDebug () << " [" << _peerAddress << " ] WebSocket message received:" << message;
40+ #endif
41+ _jsonAPI->handleMessage (message);
42+ }
43+
44+ void WebSocketJsonHandler::onBinaryMessageReceived (const QByteArray& message)
45+ {
46+ #ifdef RECEIVE_TRACE
47+ qDebug () << " [" << _peerAddress << " ] WebSocket message received:" << message.toHex ();
48+ #endif
49+ Warning (_log," Unexpected binary message received" );
3550}
3651
3752qint64 WebSocketJsonHandler::sendMessage (QJsonObject obj)
3853{
39- QString const message = JsonUtils::jsonValueToQString (obj);
40- qDebug () << " WebSocket send message: " << message;
41- return _websocket->sendTextMessage (message);
54+ #ifdef TRACE_SEND
55+ qDebug () << " [" << _peerAddress << " ] WebSocket send message: " << obj;
56+ #endif
57+ return _websocket->sendTextMessage (JsonUtils::jsonValueToQString (obj));
4258}
4359
4460void WebSocketJsonHandler::onDisconnected ()
4561{
46- qDebug () << " WebSocket disconnected" ;
62+ Debug (_log, " WebSocket disconnected from %s initiated via: %s " , QSTRING_CSTR (_peerAddress), QSTRING_CSTR (_origin)) ;
4763}
48-
49-
0 commit comments