add http and websocket

This commit is contained in:
xyz
2025-06-10 20:17:39 +08:00
parent 030b5d6101
commit 68cd53a80c
23 changed files with 650 additions and 171 deletions

View File

@ -1,4 +1,4 @@
#include "datacenter.h"
#include "datacenter.h"
namespace model
{
@ -9,11 +9,144 @@ namespace model
if (instance == nullptr) {
instance = new DataCenter();
}
return nullptr;
return instance;
}
DataCenter::DataCenter()
DataCenter::~DataCenter()
{
//释放所有的成员
//此处就不必判断nullptr直接delete即可
//c++标准中明确规定了对nullptr进行delete是合法行为不会有任何副作用
delete myself;
delete friendList;
delete chatSessionList;
delete memberList;
delete applyList;
delete recentMessages;
delete unreadMessageCount;
delete searchUserResult;
delete searchMessageResult;
}
}
DataCenter::DataCenter() : netClient(this)
{
//此处只是把这几个hash类型的属性进行new出实例其他的QList都暂时不实例化
//主要是为了使用nullptr表示非法状态
//对于hash来说不关心整个的QHash是否是nullptr而是关心某个key对应的value是否存在
//通过key是否存在也能表示该值是否有效
recentMessages = new QHash<QString, QList<Message>>();
memberList = new QHash<QString, QList<UserInfo>>();
unreadMessageCount = new QHash<QString, int>();
//加载数据
loadDataFile();
}
void DataCenter::initDataFile()
{
//构造出文件的路径使用appData存储文件
QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString filePath = basePath + "/ChatClient.json";
QDir dir;
if (!dir.exists(basePath)) {
//没有则进行目录的创建
dir.mkpath(basePath);
}
//构造好文件的路径之后,把文件创建出来
//写的方式打开,并且写入初始内容
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
LOG() << "initDataFile open file failed!!!" << file.errorString();
file.close();
return;
}
//打开成功,写入初始内容
QString data = "{\n\n}";
file.write(data.toUtf8());
file.close();
}
void DataCenter::saveDataFile()
{
QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString filePath = basePath + "/ChatClient.json";
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
LOG() << "saveDataFile open file failed!!!" << file.errorString();
file.close();
return;
}
//按照json格式进行数据的写入
//这个对象可以当作map一样来使用
QJsonObject jsonObj;
jsonObj["loginSessionId"] = loginSessionId;
QJsonObject jsonUnread;
for (auto it = unreadMessageCount->begin(); it != unreadMessageCount->end(); ++it) {
//注意此处的qt迭代器使用和stl有区别不是使用first和second
jsonUnread[it.key()] = it.value();
}
jsonObj["unread"] = jsonUnread;
//把json写入文件
QJsonDocument jsonDoc(jsonObj);
QString s = jsonDoc.toJson();
file.write(s.toUtf8());
//关闭文件
file.close();
}
//加载文件在DataCenter实例化就应该执行的
void DataCenter::loadDataFile()
{
//确保加载之前就针对文件进行初始化操作
QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString filePath = basePath + "/ChatClient.json";
//判定文件是否存在不存在则初始化并创建出空空白的json文件
QFileInfo fileInfo(filePath);
if (!fileInfo.exists()) {
initDataFile();
}
//按读方式打开文件
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
LOG() << "loadDataFile open file failed!!!" << file.errorString();
return;
}
//读取文件内容,解析为 JSON对象
QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());
if (jsonDoc.isNull()) {
LOG() << "loadDataFile parsing the json file failed!!!";
file.close();
return;
}
QJsonObject jsonObj = jsonDoc.object();
this->loginSessionId = jsonObj["loginSessionId"].toString();
this->unreadMessageCount->clear();
QJsonObject jsonUnread = jsonObj["unread"].toObject();
for (auto it = jsonUnread.begin(); it != jsonUnread.end(); ++it) {
this->unreadMessageCount->insert(it.key(), it.value().toInt());
}
file.close();
}
void DataCenter::getMyselfAsync()
{
//DataCenter 只是负责“处理数据”,
//而真正负责网络进行通信需要通过NetClient
netClient.getMyself(loginSessionId);
}
} //end namespace model