mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
refactor: 大规模调整项目目录结构,将ChatClient和ChatServer整合为Monorepo结构,并分为两个独立文件夹:chatclient/ 和 chatserver/。更新了ChatClient的CMakeLists.txt配置以适配新结构。
This commit is contained in:
132
ChatClient/include/network/netclient.h
Normal file
132
ChatClient/include/network/netclient.h
Normal file
@ -0,0 +1,132 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QWebSocket>
|
||||
#include <QProtobufSerializer>
|
||||
#include <qnetworkreply.h>
|
||||
#include <QUuid>
|
||||
|
||||
#include "../model/data.h"
|
||||
|
||||
//此处为了避免“循环包含”的问题,就需要使用前置声明的方法
|
||||
// 代替包含头文件
|
||||
namespace model {
|
||||
class DataCenter;
|
||||
} //end namespace model
|
||||
|
||||
class model::DataCenter;
|
||||
|
||||
namespace network {
|
||||
class NetClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
//定义重要的常量,ip暂时使用本地的回环ip,端口号暂定的8000和8001
|
||||
const QString HTTP_URL = "http://127.0.0.1:8000";
|
||||
const QString WEBSOCKET_URL = "ws://127.0.0.1:8001/ws";
|
||||
|
||||
public:
|
||||
NetClient(model::DataCenter* dataCenter);
|
||||
|
||||
//验证网络的联通性
|
||||
void ping();
|
||||
|
||||
//初始化websocket
|
||||
void initWebSocket();
|
||||
|
||||
//针对websocket的处理
|
||||
void handleWsResponse(const bite_im::NotifyMessage& notifyMessage);
|
||||
void handleWsMessage(const model::Message& message);
|
||||
void handleWsRemoveFriend(const QString& userId);
|
||||
void handleWsAddfriendApply(const model::UserInfo& userInfo);
|
||||
void handleWsAddFriendProcess(const model::UserInfo& userInfo, bool agree);
|
||||
void handleWsSessionCreate(const model::ChatSessionInfo& chatSessionInfo);
|
||||
|
||||
//发送身份认证请求
|
||||
void sendAuth();
|
||||
|
||||
//生成请求的Id
|
||||
static QString makeRequestId();
|
||||
|
||||
//封装发送请求的逻辑
|
||||
QNetworkReply* sendHttpRequest(const QString& apiPath, const QByteArray& body);
|
||||
|
||||
//封装处理响应的逻辑(判定HTTP正确性,反序列化,判断业务的正确性)
|
||||
//由于不同的api返回的pb对象结构不同,为了让一个函数能够处理多种不同的类型,需要使用模板
|
||||
//后面两个是输出型参数,用于表示这次的操作是成功还是失败
|
||||
template <typename T>
|
||||
std::shared_ptr<T> handleHttpResponse(QNetworkReply* httpResp, bool* ok, QString* reason) {
|
||||
//判定HTTP层面上
|
||||
if (httpResp->error() != QNetworkReply::NoError) {
|
||||
*ok = false;
|
||||
*reason = httpResp->errorString();
|
||||
httpResp->deleteLater();
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
|
||||
//说明并没有出错, 那就获取到响应的body
|
||||
QByteArray respBody = httpResp->readAll();
|
||||
|
||||
//针对body反序列化
|
||||
std::shared_ptr<T> respObj = std::make_shared<T>();
|
||||
respObj->deserialize(&serializer, respBody);
|
||||
|
||||
//判定业务的结构是否正确
|
||||
if (!respObj->success()) {
|
||||
*ok = false;
|
||||
*reason = respObj->errmsg();
|
||||
httpResp->deleteLater();
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
|
||||
//释放httpResp对象
|
||||
httpResp->deleteLater();
|
||||
*ok = true;
|
||||
return respObj;
|
||||
}
|
||||
|
||||
void getMyself(const QString& loginSessionId);
|
||||
void getFriendList(const QString& loginSessionId);
|
||||
void getChatSessionList(const QString& loginSessionId);
|
||||
void getApplyList(const QString& loginSessionId);
|
||||
void getRecentMessageList(const QString& loginSessionId, const QString& chatSessionId, bool updateUI);
|
||||
void sendMessage(const QString& loginSessionId, const QString& chatSessionId, model::MessageType messageType,
|
||||
const QByteArray& content, const QString& extraInfo);
|
||||
void receiveMessage(const QString& chatSessionId);
|
||||
void changeNickname(const QString& loginSessionId, const QString& nickname);
|
||||
void changeDescription(const QString& loginSessionId, const QString& desc);
|
||||
void getVerifyCode(const QString& email);
|
||||
void changeEmail(const QString& loginSessionId, const QString& email, const QString& verifyCodeId, const QString& verifyCode);
|
||||
void changeAvatar(const QString& loginSessionId, const QByteArray& avatar);
|
||||
void deleteFriend(const QString& loginSessionId, const QString& userId);
|
||||
void addFriendApply(const QString& loginSessionId, const QString& userId);
|
||||
void acceptFriendApply(const QString& loginSessionId, const QString& userId);
|
||||
void rejectFriendApply(const QString& loginSessionId, const QString& userId);
|
||||
void createGroupChatSession(const QString& loginSessionId, const QList<QString>& userIdList);
|
||||
void getMemberList(const QString& loginSessionId, const QString& chatSessionId);
|
||||
void searchUser(const QString& loginSessionId, const QString& searchKey);
|
||||
void searchMessage(const QString& loginSessionId, const QString& chatSessionId, const QString& searchKey);
|
||||
void searchMessageByTime(const QString& loginSessionId, const QString& chatSessionId, const QDateTime& begTime, const QDateTime& endTime);
|
||||
void userLogin(const QString& username, const QString& password);
|
||||
void userRegister(const QString& username, const QString& password);
|
||||
void phoneLogin(const QString& phone, const QString& verifyCodeId, const QString& verifyCode);
|
||||
void phoneRegister(const QString& phone, const QString& verifyCodeId, const QString& verifyCode);
|
||||
void getSingleFile(const QString& loginSessionId, const QString& fileId);
|
||||
void speechConvertText(const QString& loginSessionId, const QString& fileId, const QByteArray& content);
|
||||
|
||||
private:
|
||||
model::DataCenter* dataCenter;
|
||||
|
||||
//http客户端
|
||||
QNetworkAccessManager httpClient;
|
||||
|
||||
//websocket客户端
|
||||
QWebSocket websocketClient;
|
||||
|
||||
//序列化器
|
||||
QProtobufSerializer serializer;
|
||||
signals:
|
||||
};
|
||||
} //end namespace network
|
||||
Reference in New Issue
Block a user