mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
add http and websocket
This commit is contained in:
@ -9,6 +9,14 @@
|
||||
#include <QUuid>
|
||||
|
||||
#include "base.qpb.h"
|
||||
#include "gateway.qpb.h"
|
||||
#include "user.qpb.h"
|
||||
#include "friend.qpb.h"
|
||||
#include "file.qpb.h"
|
||||
#include "notify.qpb.h"
|
||||
#include "speech_recognition.qpb.h"
|
||||
#include "message_storage.qpb.h"
|
||||
#include "message_transmit.qpb.h"
|
||||
|
||||
// 创建命名空间
|
||||
namespace model {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <qstandardpaths.h>
|
||||
#include <QDir>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "data.h"
|
||||
#include "../network/netclient.h"
|
||||
|
||||
namespace model
|
||||
{
|
||||
@ -13,12 +17,13 @@ namespace model
|
||||
public:
|
||||
static DataCenter* getInstance();
|
||||
|
||||
~DataCenter();
|
||||
/// <summary>
|
||||
/// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ͡<EFBFBD>
|
||||
/// 计算两个整数的和。
|
||||
/// </summary>
|
||||
/// <param name="a"><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <param name="b"><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <returns><EFBFBD><EFBFBD><EFBFBD><EFBFBD> a + b <EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD></returns>
|
||||
/// <param name="a">第一个加数</param>
|
||||
/// <param name="b">第二个加数</param>
|
||||
/// <returns>返回 a + b 的结果</returns>
|
||||
/*int add(int a, int b) {
|
||||
return a + b;
|
||||
}*/
|
||||
@ -28,29 +33,70 @@ namespace model
|
||||
|
||||
static DataCenter* instance;
|
||||
|
||||
//<EFBFBD>г<EFBFBD>DataCenter<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>֯<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//列出DataCenter中要组织管理的所有数据
|
||||
|
||||
//<EFBFBD><EFBFBD>ǰ<EFBFBD>ͻ<EFBFBD><EFBFBD>˵<EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ĵ<EFBFBD>¼<EFBFBD>ỰId
|
||||
//当前客户端登录到服务器对应的登录会话Id
|
||||
QString loginSessionId;
|
||||
|
||||
//<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>Ϣ
|
||||
//当前的用户信息
|
||||
UserInfo* myself = nullptr;
|
||||
|
||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
||||
QList<UserInfo>* firendList = nullptr;
|
||||
//好友列表
|
||||
QList<UserInfo>* friendList = nullptr;
|
||||
|
||||
//<EFBFBD>Ự<EFBFBD>б<EFBFBD>
|
||||
//会话列表
|
||||
QList<ChatSessionInfo>* chatSessionList = nullptr;
|
||||
|
||||
//<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD>ǰѡ<EFBFBD>еĻỰ<EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>
|
||||
//记录当前选中的会话是哪个
|
||||
QString currentChatSession = "";
|
||||
|
||||
//<EFBFBD><EFBFBD>¼ÿ<EFBFBD><EFBFBD><EFBFBD>Ự<EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><EFBFBD>Ա
|
||||
//记录每个会话中,都有哪些成员
|
||||
QHash<QString, QList<UserInfo>>* memberList = nullptr;//unordered_map
|
||||
|
||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
||||
//待处理的好友申请列表
|
||||
QList<UserInfo>* applyList = nullptr;
|
||||
|
||||
//每个会话最近消息的列表,key为chatSessionId,value为消息列表
|
||||
QHash<QString, QList<Message>>* recentMessages = nullptr;
|
||||
|
||||
//存储每个会话,表示未读消息的数量
|
||||
QHash<QString, int>* unreadMessageCount = nullptr;
|
||||
|
||||
// 用户的好友搜索结果
|
||||
QList<UserInfo>* searchUserResult = nullptr;
|
||||
|
||||
//保存一个历史消息搜索结果
|
||||
QList<Message>* searchMessageResult = nullptr;
|
||||
|
||||
//短信(邮箱)验证码的验证Id
|
||||
QString currentVerifyCodeId = "";
|
||||
|
||||
//让dataCenter持有Netclient实例
|
||||
network::NetClient netClient;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// 初始化数据文件
|
||||
/// </summary>
|
||||
void initDataFile();
|
||||
|
||||
//存储数据到文件中
|
||||
void saveDataFile();
|
||||
|
||||
//从数据文件中加载数据到内存
|
||||
void loadDataFile();
|
||||
|
||||
//获取到当前的登录会话Id
|
||||
const QString& getLoginSessionId() const{
|
||||
return loginSessionId;
|
||||
}
|
||||
|
||||
//验证网络的连通性
|
||||
void ping() { netClient.ping(); }
|
||||
|
||||
//通过网络获取到用户的个人信息
|
||||
void getMyselfAsync();
|
||||
|
||||
signals:
|
||||
};
|
||||
} //end namespace model
|
||||
|
||||
Reference in New Issue
Block a user