2021-01-24 08:04:48 +00:00
|
|
|
|
#include "frmwebclient.h"
|
|
|
|
|
#include "ui_frmwebclient.h"
|
2023-10-17 03:31:58 +00:00
|
|
|
|
#include "qthelper.h"
|
|
|
|
|
#include "qthelperdata.h"
|
2021-01-24 08:04:48 +00:00
|
|
|
|
|
|
|
|
|
frmWebClient::frmWebClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmWebClient)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
this->initForm();
|
|
|
|
|
this->initConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frmWebClient::~frmWebClient()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 02:57:02 +00:00
|
|
|
|
bool frmWebClient::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
//双击清空
|
|
|
|
|
if (watched == ui->txtMain->viewport()) {
|
|
|
|
|
if (event->type() == QEvent::MouseButtonDblClick) {
|
|
|
|
|
on_btnClear_clicked();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 08:04:48 +00:00
|
|
|
|
void frmWebClient::initForm()
|
|
|
|
|
{
|
2021-08-22 02:57:02 +00:00
|
|
|
|
QFont font;
|
|
|
|
|
font.setPixelSize(16);
|
|
|
|
|
ui->txtMain->setFont(font);
|
|
|
|
|
ui->txtMain->viewport()->installEventFilter(this);
|
|
|
|
|
|
2021-01-24 08:04:48 +00:00
|
|
|
|
isOk = false;
|
2021-08-21 03:19:00 +00:00
|
|
|
|
|
|
|
|
|
//实例化对象并绑定信号槽
|
2021-01-24 08:04:48 +00:00
|
|
|
|
socket = new QWebSocket("WebSocket", QWebSocketProtocol::VersionLatest, this);
|
|
|
|
|
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
|
|
|
|
|
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
2021-08-22 02:57:02 +00:00
|
|
|
|
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
|
|
|
|
|
//暂时使用前面两个信号,部分系统上后面两个信号Qt没实现,目前测试到5.15.2
|
|
|
|
|
//在win上如果两组信号都关联了则都会触发,另外一组信号就是多个参数表示是否是最后一个数据包
|
|
|
|
|
connect(socket, SIGNAL(textMessageReceived(QString)), this, SLOT(textMessageReceived(QString)));
|
|
|
|
|
connect(socket, SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(binaryMessageReceived(QByteArray)));
|
|
|
|
|
//connect(socket, SIGNAL(textFrameReceived(QString, bool)), this, SLOT(textFrameReceived(QString, bool)));
|
|
|
|
|
//connect(socket, SIGNAL(binaryFrameReceived(QByteArray, bool)), this, SLOT(binaryFrameReceived(QByteArray, bool)));
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
//定时器发送数据
|
2021-01-24 08:04:48 +00:00
|
|
|
|
timer = new QTimer(this);
|
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
//填充数据到下拉框
|
|
|
|
|
ui->cboxInterval->addItems(AppData::Intervals);
|
|
|
|
|
ui->cboxData->addItems(AppData::Datas);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::initConfig()
|
|
|
|
|
{
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckHexSend->setChecked(AppConfig::HexSendWebClient);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckHexReceive->setChecked(AppConfig::HexReceiveWebClient);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckAscii->setChecked(AppConfig::AsciiWebClient);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckDebug->setChecked(AppConfig::DebugWebClient);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->ckAutoSend->setChecked(AppConfig::AutoSendWebClient);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalWebClient)));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->txtServerIP->setText(AppConfig::WebServerIP);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
ui->txtServerPort->setText(QString::number(AppConfig::WebServerPort));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
this->initTimer();
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::saveConfig()
|
|
|
|
|
{
|
2021-04-13 01:36:22 +00:00
|
|
|
|
AppConfig::HexSendWebClient = ui->ckHexSend->isChecked();
|
|
|
|
|
AppConfig::HexReceiveWebClient = ui->ckHexReceive->isChecked();
|
|
|
|
|
AppConfig::AsciiWebClient = ui->ckAscii->isChecked();
|
|
|
|
|
AppConfig::DebugWebClient = ui->ckDebug->isChecked();
|
|
|
|
|
AppConfig::AutoSendWebClient = ui->ckAutoSend->isChecked();
|
|
|
|
|
AppConfig::IntervalWebClient = ui->cboxInterval->currentText().toInt();
|
|
|
|
|
AppConfig::WebServerIP = ui->txtServerIP->text().trimmed();
|
|
|
|
|
AppConfig::WebServerPort = ui->txtServerPort->text().trimmed().toInt();
|
|
|
|
|
AppConfig::writeConfig();
|
2021-01-24 08:04:48 +00:00
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
this->initTimer();
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 03:19:00 +00:00
|
|
|
|
void frmWebClient::initTimer()
|
2021-01-24 08:04:48 +00:00
|
|
|
|
{
|
2021-08-21 03:19:00 +00:00
|
|
|
|
if (timer->interval() != AppConfig::IntervalWebClient) {
|
|
|
|
|
timer->setInterval(AppConfig::IntervalWebClient);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::AutoSendWebClient) {
|
2021-01-24 08:04:48 +00:00
|
|
|
|
if (!timer->isActive()) {
|
|
|
|
|
timer->start();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (timer->isActive()) {
|
|
|
|
|
timer->stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::append(int type, const QString &data, bool clear)
|
|
|
|
|
{
|
|
|
|
|
static int currentCount = 0;
|
|
|
|
|
static int maxCount = 100;
|
2023-10-27 06:31:09 +00:00
|
|
|
|
QtHelper::appendMsg(ui->txtMain, type, data, maxCount, currentCount, clear, ui->ckShow->isChecked());
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::connected()
|
|
|
|
|
{
|
|
|
|
|
isOk = true;
|
|
|
|
|
ui->btnConnect->setText("断开");
|
2023-10-27 06:31:09 +00:00
|
|
|
|
append(2, "服务器连接");
|
|
|
|
|
append(4, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort()));
|
|
|
|
|
append(4, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::disconnected()
|
|
|
|
|
{
|
|
|
|
|
isOk = false;
|
|
|
|
|
ui->btnConnect->setText("连接");
|
2023-10-27 06:31:09 +00:00
|
|
|
|
append(2, "服务器断开");
|
2021-08-22 02:57:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::error()
|
|
|
|
|
{
|
2023-10-27 06:31:09 +00:00
|
|
|
|
append(4, socket->errorString());
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::sendData(const QString &data)
|
|
|
|
|
{
|
|
|
|
|
QByteArray buffer;
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::HexSendWebClient) {
|
2023-10-17 03:31:58 +00:00
|
|
|
|
buffer = QtHelperData::hexStrToByteArray(data);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
buffer = data.toUtf8();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::AsciiWebClient) {
|
2021-01-24 08:04:48 +00:00
|
|
|
|
socket->sendTextMessage(data);
|
|
|
|
|
} else {
|
|
|
|
|
socket->sendBinaryMessage(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
append(0, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::textFrameReceived(const QString &data, bool isLastFrame)
|
|
|
|
|
{
|
|
|
|
|
QString buffer = data;
|
|
|
|
|
append(1, buffer);
|
|
|
|
|
|
|
|
|
|
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::DebugWebClient) {
|
2021-08-21 03:19:00 +00:00
|
|
|
|
int count = AppData::Keys.count();
|
2021-01-24 08:04:48 +00:00
|
|
|
|
for (int i = 0; i < count; i++) {
|
2021-08-21 03:19:00 +00:00
|
|
|
|
if (AppData::Keys.at(i) == buffer) {
|
|
|
|
|
sendData(AppData::Values.at(i));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::binaryFrameReceived(const QByteArray &data, bool isLastFrame)
|
|
|
|
|
{
|
|
|
|
|
QString buffer;
|
2021-04-13 01:36:22 +00:00
|
|
|
|
if (AppConfig::HexReceiveWebClient) {
|
2023-10-17 03:31:58 +00:00
|
|
|
|
buffer = QtHelperData::byteArrayToHexStr(data);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
buffer = QString(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textFrameReceived(buffer, isLastFrame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::textMessageReceived(const QString &data)
|
|
|
|
|
{
|
|
|
|
|
textFrameReceived(data, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::binaryMessageReceived(const QByteArray &data)
|
|
|
|
|
{
|
|
|
|
|
binaryFrameReceived(data, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::on_btnConnect_clicked()
|
|
|
|
|
{
|
|
|
|
|
if (ui->btnConnect->text() == "连接") {
|
2021-04-13 01:36:22 +00:00
|
|
|
|
QString url = QString("%1:%2").arg(AppConfig::WebServerIP).arg(AppConfig::WebServerPort);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
socket->abort();
|
|
|
|
|
socket->open(QUrl(url));
|
|
|
|
|
} else {
|
|
|
|
|
socket->abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::on_btnSave_clicked()
|
|
|
|
|
{
|
|
|
|
|
QString data = ui->txtMain->toPlainText();
|
2021-08-21 03:19:00 +00:00
|
|
|
|
AppData::saveData(data);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
on_btnClear_clicked();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::on_btnClear_clicked()
|
|
|
|
|
{
|
|
|
|
|
append(0, "", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frmWebClient::on_btnSend_clicked()
|
|
|
|
|
{
|
|
|
|
|
if (!isOk) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString data = ui->cboxData->currentText();
|
|
|
|
|
if (data.length() <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendData(data);
|
|
|
|
|
}
|