Add support for websocket subprotocols

This commit is contained in:
2023-02-10 14:53:51 +01:00
parent 7a143c2505
commit 96b5379aba
2 changed files with 44 additions and 4 deletions

View File

@ -3,6 +3,7 @@
#include <QMessageBox>
#include <QMetaEnum>
#include <QWebSocketHandshakeOptions>
// utilities
namespace {
@ -61,7 +62,10 @@ void MainWindow::connectClicked()
.arg(QTime::currentTime().toString())
.arg(tr("Connecting to %0").arg(url.toString())));
m_webSocket.open(url);
QWebSocketHandshakeOptions options;
if (m_ui->checkBoxUseSubprotocol->isChecked())
options.setSubprotocols({m_ui->lineEditSubprotocol->text()});
m_webSocket.open(url, std::move(options));
}
else
m_webSocket.close();
@ -104,6 +108,8 @@ void MainWindow::stateChanged(QAbstractSocket::SocketState state)
{
m_ui->lineEditUrl->setEnabled(state == QAbstractSocket::UnconnectedState);
m_ui->pushButtonConnect->setText(state == QAbstractSocket::UnconnectedState ? tr("Connect") : tr("Disconnect"));
m_ui->checkBoxUseSubprotocol->setEnabled(state == QAbstractSocket::UnconnectedState);
m_ui->lineEditSubprotocol->setEnabled(state == QAbstractSocket::UnconnectedState ? m_ui->checkBoxUseSubprotocol->isChecked() : false);
m_ui->labelStatus->setText(qtEnumToString(state));
m_ui->lineEditSend->setEnabled(state == QAbstractSocket::ConnectedState);
m_ui->pushButtonSend->setEnabled(state == QAbstractSocket::ConnectedState);

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<width>1011</width>
<height>600</height>
</rect>
</property>
@ -16,7 +16,7 @@
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0,0,0,0">
<item>
<widget class="QLineEdit" name="lineEditUrl">
<property name="text">
@ -24,6 +24,23 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBoxUseSubprotocol">
<property name="text">
<string>Use subprotocol:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditSubprotocol">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>ocpp1.6</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonConnect">
<property name="text">
@ -66,5 +83,22 @@
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
<connections>
<connection>
<sender>checkBoxUseSubprotocol</sender>
<signal>toggled(bool)</signal>
<receiver>lineEditSubprotocol</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>654</x>
<y>22</y>
</hint>
<hint type="destinationlabel">
<x>787</x>
<y>22</y>
</hint>
</hints>
</connection>
</connections>
</ui>