16 #include <QTextStream> 18 #include <QMessageBox> 24 #include <QIntValidator> 25 #include <QMessageBox> 28 #include "ui_websocket_server.h" 35 ui(new
Ui::WebSocketDialog)
38 ui->lineEditPort->setValidator(
new QIntValidator() );
39 setWindowTitle(
"WebSocket Server");
41 connect(
ui->buttonBox, &QDialogButtonBox::accepted,
42 this, &QDialog::accept );
43 connect(
ui->buttonBox, &QDialogButtonBox::rejected,
44 this, &QDialog::reject );
48 while(
ui->layoutOptions->count() > 0)
50 auto item =
ui->layoutOptions->takeAt(0);
51 item->widget()->setParent(
nullptr);
55 Ui::WebSocketDialog*
ui;
60 _server(
"plotJuggler", QWebSocketServer::NonSecureMode)
62 connect(&
_server, &QWebSocketServer::newConnection,
80 QMessageBox::warning(
nullptr,tr(
"Websocket Server"), tr(
"No available MessageParsers"), QMessageBox::Ok);
91 dialog->
ui->comboBoxProtocol->addItem( it.first );
93 if(
auto widget = it.second->optionsWidget() )
95 widget->setVisible(
false);
96 dialog->
ui->layoutOptions->addWidget( widget );
102 QString protocol = settings.value(
"WebsocketServer::protocol",
"JSON").toString();
103 int port = settings.value(
"WebsocketServer::port", 9871).toInt();
105 dialog->
ui->lineEditPort->setText( QString::number(port) );
107 std::shared_ptr<MessageParserCreator> parser_creator;
109 connect(dialog->
ui->comboBoxProtocol, qOverload<const QString &>( &QComboBox::currentIndexChanged),
this,
110 [&](QString protocol)
112 if( parser_creator ){
113 QWidget* prev_widget = parser_creator->optionsWidget();
114 prev_widget->setVisible(false);
118 if(
auto widget = parser_creator->optionsWidget() ){
119 widget->setVisible(
true);
123 dialog->ui->comboBoxProtocol->setCurrentText(protocol);
125 int res = dialog->exec();
126 if( res == QDialog::Rejected )
132 port = dialog->ui->lineEditPort->text().toUShort(&ok);
133 protocol = dialog->ui->comboBoxProtocol->currentText();
134 dialog->deleteLater();
139 settings.setValue(
"WebsocketServer::protocol", protocol);
140 settings.setValue(
"WebsocketServer::port", port);
142 if (
_server.listen(QHostAddress::Any, port))
144 qDebug() <<
"Websocket listening on port" << port;
149 QMessageBox::warning(
nullptr,tr(
"Websocket Server"),
150 tr(
"Couldn't open websocket on port %1").
arg(port),
170 QWebSocket* pSocket =
_server.nextPendingConnection();
178 std::lock_guard<std::mutex> lock(
mutex());
180 using namespace std::chrono;
181 auto ts = high_resolution_clock::now().time_since_epoch();
182 double timestamp = 1e-6* double( duration_cast<microseconds>(ts).
count() );
184 QByteArray bmsg = message.toLocal8Bit();
185 MessageRef msg ( reinterpret_cast<uint8_t*>(bmsg.data()), bmsg.size() );
188 _parser->parseMessage(msg, timestamp);
189 }
catch (std::exception& err)
191 QMessageBox::warning(
nullptr,
192 tr(
"Websocket Server"),
193 tr(
"Problem parsing the message. Websocket Server will be stopped.\n%1").
arg(err.what()),
205 QWebSocket* pClient = qobject_cast<QWebSocket*>(sender());
211 pClient->deleteLater();
PJ::MessageParserPtr _parser
virtual ~WebsocketServer() override
MessageParserFactory * availableParsers()
QList< QWebSocket * > _clients
PlotDataMapRef & dataMap()
detail::named_arg< Char, T > arg(const Char *name, const T &arg)
virtual void shutdown() override
void socketDisconnected()
virtual bool start(QStringList *) override
void processMessage(QString message)