55#include " QtHttpReply.h"
66#include " QtHttpServer.h"
77#include " QtHttpHeader.h"
8- #include " WebSocketClient .h"
8+ #include " WebSocketJsonHandler .h"
99#include " WebJsonRpc.h"
1010
1111#include < QCryptographicHash>
@@ -29,6 +29,8 @@ QtHttpClientWrapper::QtHttpClientWrapper (QTcpSocket * sock, const bool& localCo
2929 , m_webJsonRpc (nullptr )
3030{
3131 connect (m_sockClient, &QTcpSocket::readyRead, this , &QtHttpClientWrapper::onClientDataReceived);
32+ connect (&m_websocketServer, &QWebSocketServer::newConnection,
33+ this , &QtHttpClientWrapper::onNewWebSocketConnection);
3234}
3335
3436QString QtHttpClientWrapper::getGuid (void )
@@ -50,6 +52,11 @@ void QtHttpClientWrapper::onClientDataReceived (void)
5052{
5153 if (m_sockClient != Q_NULLPTR)
5254 {
55+ if (!m_sockClient->isTransactionStarted ())
56+ {
57+ m_sockClient->startTransaction ();
58+ }
59+
5360 while (m_sockClient->bytesAvailable () != 0 )
5461 {
5562 QByteArray line = m_sockClient->readLine ();
@@ -162,22 +169,25 @@ void QtHttpClientWrapper::onClientDataReceived (void)
162169 {
163170 case RequestParsed: // a valid request has ben fully parsed
164171 {
165- // Catch websocket header "Upgrade"
166- if (m_currentRequest->getHeader (QtHttpHeader::Upgrade).toLower () == " websocket" )
167- {
172+ const auto & upgradeValue = m_currentRequest->getHeader (QtHttpHeader::Upgrade).toLower ();
173+ if (upgradeValue.compare (QByteArrayLiteral (" websocket" ), Qt::CaseInsensitive) == 0 ) {
174+
175+ qDebug () << " WebSocket upgrade detected, passing to QWebSocketServer" ;
176+
168177 if (m_websocketClient == Q_NULLPTR)
169178 {
170179 // disconnect this slot from socket for further requests
171180 disconnect (m_sockClient, &QTcpSocket::readyRead, this , &QtHttpClientWrapper::onClientDataReceived);
172- // disabling packet bunching
173- m_sockClient-> setSocketOption (QAbstractSocket::LowDelayOption, 1 );
174- m_sockClient->setSocketOption (QAbstractSocket::KeepAliveOption, 1 );
175- m_websocketClient = new WebSocketClient (m_currentRequest, m_sockClient, m_localConnection, this ) ;
181+ m_sockClient-> rollbackTransaction ();
182+ m_websocketServer. handleConnection (m_sockClient );
183+ emit m_sockClient->readyRead ( );
184+ return ;
176185 }
177186
178187 break ;
179188 }
180189
190+ m_sockClient->commitTransaction ();
181191 // add post data to request and catch /jsonrpc subroute url
182192 if ( m_currentRequest->getCommand () == " POST" )
183193 {
@@ -227,6 +237,8 @@ void QtHttpClientWrapper::onClientDataReceived (void)
227237 case ParsingError: // there was an error durin one of parsing steps
228238 {
229239 m_sockClient->readAll (); // clear remaining buffer to ignore content
240+ m_sockClient->commitTransaction ();
241+
230242 QtHttpReply reply (m_serverHandle);
231243 reply.setStatusCode (QtHttpReply::BadRequest);
232244 reply.appendRawData (QByteArrayLiteral (" <h1>Bad Request (HTTP parsing error) !</h1>" ));
@@ -365,3 +377,19 @@ void QtHttpClientWrapper::closeConnection()
365377 }
366378 m_sockClient->close ();
367379}
380+
381+ void QtHttpClientWrapper::onNewWebSocketConnection () {
382+
383+ // Handle the pending connection
384+ QWebSocket* webSocket = m_websocketServer.nextPendingConnection ();
385+ if (webSocket) {
386+ qDebug () << " New WebSocket connection established" ;
387+
388+ // Manage the WebSocketJsonHandler for this connection
389+ WebSocketJsonHandler* handler = new WebSocketJsonHandler (webSocket);
390+ connect (webSocket, &QWebSocket::disconnected, handler, &QObject::deleteLater);
391+ }
392+ else {
393+ qWarning () << " No pending WebSocket connection!" ;
394+ }
395+ }
0 commit comments