-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytcpsocket.cpp
94 lines (88 loc) · 4.2 KB
/
mytcpsocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "mytcpsocket.h"
#include "MyTcpServer.h"
mytcpsocket::mytcpsocket(QObject *parent, qintptr handle)
: QTcpSocket(parent)
{
this->handle = handle;
qDebug() << "传入socket" ;
connect(this, &mytcpsocket::readyRead, this, &mytcpsocket::readyReadHandle);
connect(this, &mytcpsocket::disconnected, this, &mytcpsocket::disconnectedHandle);
}
mytcpsocket::~mytcpsocket()
{}
void mytcpsocket::readyReadHandle() {
QByteArray data = this->readAll();
qDebug() << "进入readyRead"<< data;
// 将字节数组转换为 JSON 文档
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
// 解析 JSON 文档
//qDebug() << "开始解析";
QJsonObject jsonObj = jsonDoc.object();
QString sqlType = jsonObj["sql_type"].toString();
qDebug() << "sqlType" << sqlType;
if ("login" == sqlType) {
QString usertype, username, password;
MyJSON::parseLogin(jsonObj, usertype, username, password);
// 处理登录逻辑
qDebug() << "usertype" << usertype << "username" << username << "password" << password;
emit loginSiganl(usertype, username, password, handle);
}
else if ("register" == sqlType) {
}
else if ("select" == sqlType) {
QString messagetype, registertype, information;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, messagetype, registertype, information);
qDebug() << "registertype"<< registertype;
emit infRegSiganl(messagetype, registertype, information, handle, messageType);
} else if ("insert" == sqlType) {
QString messagetype, fieldNames, fieldValues;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, messagetype, fieldNames, fieldValues);
emit questionInsertSiganl(messagetype, fieldNames, fieldValues, handle, messageType);
} else if ("mulTableInsert" == sqlType) {
QString value1, value2, value3;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, value1, value2, value3);
emit mulTableInsertSiganl(value1, value2, value3, handle, messageType);
} else if ("delectQuestion" == sqlType) {
QString messagetype, fieldNames, fieldValues;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, messagetype, fieldNames, fieldValues);
emit questionDelectSiganl(messagetype, fieldNames, fieldValues, handle, messageType);
} else if ("delect" == sqlType) {
QString messagetype, fieldNames, fieldValues;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, messagetype, fieldNames, fieldValues);
emit delectSiganl(messagetype, fieldNames, fieldValues, handle, messageType);
} else if ("update" == sqlType) {
QString tableName, fieldNames, fieldValues;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, tableName, fieldNames, fieldValues);
qDebug() << "emit updateSiganl";
emit questionUpdateSiganl(tableName, fieldNames, fieldValues, handle, messageType);
} else if ("qqInsert" == sqlType) {
QString tableName, fieldNames, fieldValues;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, tableName, fieldNames, fieldValues);
qDebug() << "emit qqInsertSiganl";
emit qqInsertSiganl(tableName, fieldNames, fieldValues, handle, messageType);
} else if ("studentAnswer" == sqlType) {
QString tableName, fieldNames, fieldValues;
QString messageType = jsonObj["message_type"].toString();
MyJSON::parseInformation(jsonObj, tableName, fieldNames, fieldValues);
qDebug() << "emit studentAnswerSiganl";
emit studentAnswerSiganl(tableName, fieldNames, fieldValues, handle, messageType);
}
}
void mytcpsocket::disconnectedHandle()
{
emit onlineSiganl(handle);
qDebug() << "disconnectedHandle";
}
void mytcpsocket::sendHandle(QByteArray toSend)
{
qDebug() <<"发送数据" << toSend;
this->write(toSend);
this->waitForBytesWritten(-1); //阻塞发送
}