19 #include <QTextStream>
21 #include <QMessageBox>
27 #include <QIntValidator>
28 #include <QMessageBox>
31 #include "ui_websocket_server.h"
39 ui->lineEditPort->setValidator(
new QIntValidator());
40 setWindowTitle(
"WebSocket Server");
42 connect(
ui->buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept);
43 connect(
ui->buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject);
47 while (
ui->layoutOptions->count() > 0)
49 auto item =
ui->layoutOptions->takeAt(0);
50 item->widget()->setParent(
nullptr);
54 Ui::WebSocketDialog*
ui;
58 : _running(false), _server(
"plotJuggler", QWebSocketServer::NonSecureMode)
60 connect(&
_server, &QWebSocketServer::newConnection,
this,
78 QMessageBox::warning(
nullptr, tr(
"Websocket Server"),
79 tr(
"No available MessageParsers"), QMessageBox::Ok);
90 dialog->
ui->comboBoxProtocol->addItem(it.first);
92 if (
auto widget = it.second->optionsWidget())
94 widget->setVisible(
false);
95 dialog->
ui->layoutOptions->addWidget(widget);
101 QString protocol = settings.value(
"WebsocketServer::protocol",
"JSON").toString();
102 int port = settings.value(
"WebsocketServer::port", 9871).toInt();
104 dialog->
ui->lineEditPort->setText(QString::number(port));
108 connect(dialog->
ui->comboBoxProtocol,
109 qOverload<const QString&>(&QComboBox::currentIndexChanged),
this,
110 [&](
const QString& selected_protocol) {
113 if (auto prev_widget = parser_creator->optionsWidget())
115 prev_widget->setVisible(false);
120 if (
auto widget = parser_creator->optionsWidget())
122 widget->setVisible(true);
126 dialog->ui->comboBoxProtocol->setCurrentText(protocol);
128 int res = dialog->exec();
129 if (res == QDialog::Rejected)
135 port = dialog->ui->lineEditPort->text().toUShort(&ok);
136 protocol = dialog->ui->comboBoxProtocol->currentText();
137 dialog->deleteLater();
139 _parser = parser_creator->createParser({}, {}, {}, dataMap());
142 settings.setValue(
"WebsocketServer::protocol", protocol);
143 settings.setValue(
"WebsocketServer::port", port);
145 if (_server.listen(QHostAddress::Any, port))
147 qDebug() <<
"Websocket listening on port" << port;
152 QMessageBox::warning(
nullptr, tr(
"Websocket Server"),
153 tr(
"Couldn't open websocket on port %1").
arg(port),
173 QWebSocket* pSocket =
_server.nextPendingConnection();
174 connect(pSocket, &QWebSocket::textMessageReceived,
this,
182 std::lock_guard<std::mutex> lock(
mutex());
184 using namespace std::chrono;
185 auto ts = high_resolution_clock::now().time_since_epoch();
186 double timestamp = 1e-6 * double(duration_cast<microseconds>(ts).
count());
188 QByteArray bmsg = message.toLocal8Bit();
189 MessageRef msg(
reinterpret_cast<uint8_t*
>(bmsg.data()), bmsg.size());
195 catch (std::exception& err)
197 QMessageBox::warning(
nullptr, tr(
"Websocket Server"),
198 tr(
"Problem parsing the message. Websocket Server will be "
212 QWebSocket* pClient = qobject_cast<QWebSocket*>(sender());
215 disconnect(pClient, &QWebSocket::textMessageReceived,
this,
217 disconnect(pClient, &QWebSocket::disconnected,
this,
220 pClient->deleteLater();