Some improvements #2
2
.gitignore
vendored
2
.gitignore
vendored
@@ -71,3 +71,5 @@ Thumbs.db
|
||||
*.dll
|
||||
*.exe
|
||||
|
||||
# Build files
|
||||
/build*/
|
||||
|
125
mainwindow.cpp
125
mainwindow.cpp
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QMetaEnum>
|
||||
#include <QSettings>
|
||||
|
||||
// utilities
|
||||
namespace {
|
||||
@@ -23,9 +24,14 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
connect(m_ui->lineEditUrl, &QLineEdit::returnPressed, this, &MainWindow::connectClicked);
|
||||
connect(m_ui->pushButtonConnect, &QAbstractButton::clicked, this, &MainWindow::connectClicked);
|
||||
|
||||
connect(m_ui->lineEditSend, &QLineEdit::returnPressed, this, &MainWindow::sendClicked);
|
||||
connect(m_ui->comboBoxSend->lineEdit(), &QLineEdit::returnPressed, this, &MainWindow::sendClicked);
|
||||
connect(m_ui->pushButtonSend, &QAbstractButton::clicked, this, &MainWindow::sendClicked);
|
||||
|
||||
connect(m_ui->saveSlot, &QComboBox::currentIndexChanged, this, &MainWindow::loadSelectedUrl);
|
||||
connect(m_ui->pushButtonSave, &QAbstractButton::clicked, this, &MainWindow::saveSettings);
|
||||
|
||||
connect(m_ui->autoReconnectBox, &QCheckBox::toggled, this, &MainWindow::setAutoReconnect);
|
||||
|
||||
connect(&m_webSocket, &QWebSocket::connected, this, &MainWindow::connected);
|
||||
connect(&m_webSocket, &QWebSocket::disconnected, this, &MainWindow::disconnected);
|
||||
connect(&m_webSocket, &QWebSocket::stateChanged, this, &MainWindow::stateChanged);
|
||||
@@ -34,7 +40,15 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
connect(&m_webSocket, qOverload<QAbstractSocket::SocketError>(&QWebSocket::error), this, &MainWindow::error);
|
||||
connect(&m_webSocket, &QWebSocket::pong, this, &MainWindow::pong);
|
||||
|
||||
for (uint8_t i = 1; i <= m_url_slots.size(); i++) {
|
||||
m_ui->saveSlot->addItem("Slot "+QString::number(i));
|
||||
}
|
||||
|
||||
loadSettings();
|
||||
|
||||
stateChanged(m_webSocket.state());
|
||||
|
||||
qApp->installEventFilter(this);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() = default;
|
||||
@@ -43,6 +57,37 @@ void MainWindow::connectClicked()
|
||||
{
|
||||
if (m_webSocket.state() == QAbstractSocket::UnconnectedState)
|
||||
{
|
||||
connectToWebsocket();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_ui->autoReconnectBox->setChecked(false);
|
||||
m_autoReconnect = false;
|
||||
m_webSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::sendClicked()
|
||||
{
|
||||
if (m_webSocket.state() != QAbstractSocket::ConnectedState)
|
||||
{
|
||||
QMessageBox::warning(this, tr("WebSocket not connected!"), tr("WebSocket not connected!"));
|
||||
return;
|
||||
}
|
||||
|
||||
const auto msg = m_ui->comboBoxSend->currentText();
|
||||
m_webSocket.sendTextMessage(msg);
|
||||
m_ui->comboBoxSend->setCurrentIndex(m_ui->comboBoxSend->count());
|
||||
|
||||
m_ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <span style=\"color: %1;\">%2</span>: %3<br/>")
|
||||
.arg(QTime::currentTime().toString())
|
||||
.arg("red")
|
||||
.arg(tr("SEND"))
|
||||
.arg(msg));
|
||||
}
|
||||
|
||||
void MainWindow::connectToWebsocket()
|
||||
{
|
||||
const auto url = QUrl::fromUserInput(m_ui->lineEditUrl->text());
|
||||
if (url.isEmpty())
|
||||
{
|
||||
@@ -62,28 +107,6 @@ void MainWindow::connectClicked()
|
||||
.arg(tr("Connecting to %0").arg(url.toString())));
|
||||
|
||||
m_webSocket.open(url);
|
||||
}
|
||||
else
|
||||
m_webSocket.close();
|
||||
}
|
||||
|
||||
void MainWindow::sendClicked()
|
||||
{
|
||||
if (m_webSocket.state() != QAbstractSocket::ConnectedState)
|
||||
{
|
||||
QMessageBox::warning(this, tr("WebSocket not connected!"), tr("WebSocket not connected!"));
|
||||
return;
|
||||
}
|
||||
|
||||
const auto msg = m_ui->lineEditSend->text();
|
||||
m_webSocket.sendTextMessage(msg);
|
||||
m_ui->lineEditSend->clear();
|
||||
|
||||
m_ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <span style=\"color: %1;\">%2</span>: %3<br/>")
|
||||
.arg(QTime::currentTime().toString())
|
||||
.arg("red")
|
||||
.arg(tr("SEND"))
|
||||
.arg(msg));
|
||||
}
|
||||
|
||||
void MainWindow::connected()
|
||||
@@ -91,6 +114,7 @@ void MainWindow::connected()
|
||||
m_ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <i>%1</i><br/>")
|
||||
.arg(QTime::currentTime().toString())
|
||||
.arg(tr("Connected")));
|
||||
m_autoReconnectTries = 0;
|
||||
}
|
||||
|
||||
void MainWindow::disconnected()
|
||||
@@ -98,14 +122,32 @@ void MainWindow::disconnected()
|
||||
m_ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <i>%1</i><br/>")
|
||||
.arg(QTime::currentTime().toString())
|
||||
.arg(tr("Disconnected")));
|
||||
if (m_autoReconnect && m_autoReconnectTries < 5)
|
||||
{
|
||||
m_ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <i>%1</i><br/>")
|
||||
.arg(QTime::currentTime().toString())
|
||||
.arg(tr("Auto-Reconnecting...")));
|
||||
connectToWebsocket();
|
||||
m_autoReconnectTries++;
|
||||
}
|
||||
else if (m_autoReconnectTries >= 5)
|
||||
{
|
||||
m_ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <i>%1</i><br/>")
|
||||
.arg(QTime::currentTime().toString())
|
||||
.arg(tr("Maximum attempts reached.")));
|
||||
m_ui->autoReconnectBox->setChecked(false);
|
||||
m_autoReconnect = false;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::stateChanged(QAbstractSocket::SocketState state)
|
||||
{
|
||||
m_ui->lineEditUrl->setEnabled(state == QAbstractSocket::UnconnectedState);
|
||||
m_ui->saveSlot->setEnabled(state == QAbstractSocket::UnconnectedState);
|
||||
m_ui->pushButtonSave->setEnabled(state == QAbstractSocket::UnconnectedState);
|
||||
m_ui->pushButtonConnect->setText(state == QAbstractSocket::UnconnectedState ? tr("Connect") : tr("Disconnect"));
|
||||
m_ui->labelStatus->setText(qtEnumToString(state));
|
||||
m_ui->lineEditSend->setEnabled(state == QAbstractSocket::ConnectedState);
|
||||
m_ui->comboBoxSend->setEnabled(state == QAbstractSocket::ConnectedState);
|
||||
m_ui->pushButtonSend->setEnabled(state == QAbstractSocket::ConnectedState);
|
||||
}
|
||||
|
||||
@@ -136,3 +178,38 @@ void MainWindow::pong(quint64 elapsedTime, const QByteArray &payload)
|
||||
{
|
||||
qDebug() << "pong" << elapsedTime;
|
||||
}
|
||||
|
||||
void MainWindow::saveSettings()
|
||||
{
|
||||
QSettings settings;
|
||||
m_url_slots[m_ui->saveSlot->currentIndex()] = m_ui->lineEditUrl->text();
|
||||
for (uint8_t i = 1; i <= m_url_slots.size(); i++) {
|
||||
settings.setValue("slot"+QString::number(i-1), m_url_slots[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::loadSettings()
|
||||
{
|
||||
QSettings settings;
|
||||
for (uint8_t i = 1; i <= m_url_slots.size(); i++) {
|
||||
QString key = "slot"+QString::number(i-1);
|
||||
m_url_slots[i-1] = settings.value(key, "ws://localhost:1234/path/to/ws").toString();
|
||||
}
|
||||
loadSelectedUrl();
|
||||
saveSettings();
|
||||
}
|
||||
|
||||
void MainWindow::loadSelectedUrl()
|
||||
{
|
||||
const auto tmpSocketState = m_webSocket.state();
|
||||
m_webSocket.close();
|
||||
m_ui->lineEditUrl->setText(m_url_slots[m_ui->saveSlot->currentIndex()]);
|
||||
|
||||
if (tmpSocketState == QAbstractSocket::ConnectedState)
|
||||
connectToWebsocket();
|
||||
}
|
||||
|
||||
void MainWindow::setAutoReconnect(bool state)
|
||||
{
|
||||
m_autoReconnect = state;
|
||||
}
|
||||
|
11
mainwindow.h
11
mainwindow.h
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QWebSocket>
|
||||
#include <QComboBox>
|
||||
|
||||
#include <memory>
|
||||
|
||||
@@ -18,6 +19,7 @@ public:
|
||||
private slots:
|
||||
void connectClicked();
|
||||
void sendClicked();
|
||||
void connectToWebsocket();
|
||||
|
||||
void connected();
|
||||
void disconnected();
|
||||
@@ -27,7 +29,16 @@ private slots:
|
||||
void error(QAbstractSocket::SocketError error);
|
||||
void pong(quint64 elapsedTime, const QByteArray &payload);
|
||||
|
||||
void saveSettings();
|
||||
void loadSettings();
|
||||
void loadSelectedUrl();
|
||||
|
||||
void setAutoReconnect(bool state);
|
||||
|
||||
private:
|
||||
const std::unique_ptr<Ui::MainWindow> m_ui;
|
||||
QWebSocket m_webSocket;
|
||||
std::array<QString, 10> m_url_slots;
|
||||
bool m_autoReconnect;
|
||||
uint8_t m_autoReconnectTries{0};
|
||||
};
|
||||
|
@@ -6,10 +6,16 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
<width>995</width>
|
||||
<height>709</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>WebSocket Tester</string>
|
||||
</property>
|
||||
@@ -17,6 +23,22 @@
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QComboBox" name="saveSlot">
|
||||
<property name="editable">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="currentText">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>-1</number>
|
||||
</property>
|
||||
<property name="maxCount">
|
||||
<number>10</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEditUrl">
|
||||
<property name="text">
|
||||
@@ -24,6 +46,13 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButtonSave">
|
||||
<property name="text">
|
||||
<string>Save</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButtonConnect">
|
||||
<property name="text">
|
||||
@@ -38,15 +67,47 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="autoReconnectBox">
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>AutoReconnect</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="autoRepeat">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEditSend"/>
|
||||
<widget class="QComboBox" name="comboBoxSend">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButtonSend">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>80</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Send</string>
|
||||
</property>
|
||||
|
Reference in New Issue
Block a user