qt_demoe/tool/nettool/api/webclient.cpp

106 lines
2.9 KiB
C++
Raw Normal View History

#include "webclient.h"
2023-10-17 03:31:58 +00:00
#include "qthelper.h"
#include "qthelperdata.h"
WebClient::WebClient(QWebSocket *socket, QObject *parent) : QObject(parent)
{
this->socket = socket;
ip = socket->peerAddress().toString();
ip = ip.replace("::ffff:", "");
port = socket->peerPort();
2021-08-22 02:57:02 +00:00
connect(socket, SIGNAL(disconnected()), this, SLOT(slot_disconnected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_error()));
2021-08-22 02:57:02 +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)));
}
QString WebClient::getIP() const
{
return this->ip;
}
int WebClient::getPort() const
{
return this->port;
}
2021-08-22 02:57:02 +00:00
void WebClient::slot_disconnected()
{
2021-08-22 02:57:02 +00:00
emit disconnected(ip, port);
socket->deleteLater();
this->deleteLater();
2021-08-22 02:57:02 +00:00
}
void WebClient::slot_error()
{
emit error(ip, port, socket->errorString());
}
void WebClient::textFrameReceived(const QString &data, bool isLastFrame)
{
QString buffer = data;
emit receiveData(ip, port, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
2021-04-13 01:36:22 +00:00
if (AppConfig::DebugWebServer) {
int count = AppData::Keys.count();
for (int i = 0; i < count; i++) {
if (AppData::Keys.at(i) == buffer) {
sendData(AppData::Values.at(i));
break;
}
}
}
}
void WebClient::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);
} else {
buffer = QString(data);
}
textFrameReceived(buffer, isLastFrame);
}
void WebClient::textMessageReceived(const QString &data)
{
textFrameReceived(data, true);
}
void WebClient::binaryMessageReceived(const QByteArray &data)
{
binaryFrameReceived(data, true);
}
void WebClient::sendData(const QString &data)
{
QByteArray buffer;
2021-04-13 01:36:22 +00:00
if (AppConfig::HexSendWebServer) {
2023-10-17 03:31:58 +00:00
buffer = QtHelperData::hexStrToByteArray(data);
} else {
buffer = data.toUtf8();
}
2021-04-13 01:36:22 +00:00
if (AppConfig::AsciiWebServer) {
socket->sendTextMessage(data);
} else {
socket->sendBinaryMessage(buffer);
}
emit sendData(ip, port, data);
}
void WebClient::abort()
{
socket->abort();
}