Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYWebSocketTransport.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 #include "TYWebSocketTransport.h"
17 #include <QJsonParseError>
18 #include <QJsonObject>
19 
20 void TYWebSocketTransport::sendMessage(const QJsonObject& message)
21 {
22  QJsonDocument doc(message);
23  emit messageChanged(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
24 }
25 
26 void TYWebSocketTransport::textMessageReceive(const QString& messageData)
27 {
28  QJsonParseError error;
29  QJsonDocument message = QJsonDocument::fromJson(messageData.toUtf8(), &error);
30  if (error.error)
31  {
32  qWarning() << "Failed to parse text message as JSON object:" << messageData
33  << "Error is:" << error.errorString();
34  return;
35  }
36  else if (!message.isObject())
37  {
38  qWarning() << "Received JSON message that is not an object: " << messageData;
39  return;
40  }
41  emit messageReceived(message.object(), this);
42 }
Q_INVOKABLE void sendMessage(const QJsonObject &message) override
void messageChanged(const QString &message)
Q_INVOKABLE void textMessageReceive(const QString &messageData)