我们之前把聊天服务器的代码基本上功能开发完了,在后面转成集群版本的时候要引入中间件-基于发布订阅的Redis。
现在我们先开始客户端的开发
我们聊天服务器项目工程的客户端和服务器会共用很多代码,所以把他们放在集成编译环境中有很大的优势。当客户端和服务器共享头文件,可以同步修改,修改一份都是可见的。
为了减少服务器的压力,这种好友列表群组列表一般是在客户端存储的。如果从服务端拉回,返回数据挺多的,压力挺大的
我们完善public.hpp
#ifndef PUBLIC_H
#define PUBLIC_H
/*
server和client的公共文件
*/
enum EnMsgType
{
LOGIN_MSG = 1,//登录消息
LOGIN_MSG_ACK,//登录响应消息
LOGINOUT_MSG,//注销消息
REG_MSG,//注册消息
REG_MSG_ACK,//注册响应消息
ONE_CHAT_MSG,//聊天消息
ADD_FRIEND_MSG,//添加好友消息
CREATE_GROUP_MSG,//创建群组
ADD_GROUP_MSG,//加入群组
GROUP_CHAT_MSG,//群聊天
};
#endif
我们完善chatservice.hpp
#ifndef CHATSERVICE_H
#define CHATSERVICE_H
#include
#include //一个消息ID映射一个事件处理
#include
#include
using namespace std;
using namespace muduo;
using namespace muduo::net;
#include "groupmodel.hpp"
#include "usermodel.hpp"
#include "offlinemessagemodel.hpp"
#include "friendmodel.hpp"
#include "json.hpp"
using json = nlohmann::json;
//表示处理消息的事件回调方法类型,事件处理器,派发3个东西
using MsgHandler = std::function<void(const TcpConnectionPtr &conn, json &js, Timestamp)>;
//聊天服务器业务类
class ChatService
{
public:
//获取单例对象的接口函数
static ChatService *instance();
//处理登录业务
void login(const TcpConnectionPtr &conn, json &js, Timestamp time);
//处理注册业务
void reg(const TcpConnectionPtr &conn, json &js, Timestamp time);
//一对一聊天业务
void oneChat(const TcpConnectionPtr &conn, json &js, Timestamp time);
//添加好友业务
void addFriend(const TcpConnectionPtr &conn, json &js, Timestamp time);
//创建群组业务
void createGroup(const TcpConnectionPtr &conn, json &js, Timestamp time);
//加入群组业务
void addGroup(const TcpConnectionPtr &conn, json &js, Timestamp time);
//群组聊天业务
void groupChat(const TcpConnectionPtr &conn, json &js, Timestamp time);
//处理注销业务
void loginout(const TcpConnectionPtr &conn, json &js, Timestamp time);
//处理客户端异常退出
void clientCloseException(const TcpConnectionPtr &conn);
//服务器异常,业务重置方法
void reset();
//获取消息对应的处理器
MsgHandler getHandler(int msgid);
private:
ChatService();//单例
//存储消息id和其对应的业务处理方法,消息处理器的一个表,写消息id对应的处理操作
unordered_map<int, MsgHandler> _msgHandlerMap;
//存储在线用户的通信连接,用户的id, TcpConnectionPtr
unordered_map<int, TcpConnectionPtr> _userConnMap;
//定义互斥锁,保证_userConnMap的线程安全
mutex _connMutex;
//数据操作类对象
UserModel _userModel;
OfflineMsgModel _offlineMsgModel;
FriendModel _friendModel;
GroupModel _groupModel;
};
#endif
我们完善chatservice.cpp
#include "chatservice.hpp"
#include "public.hpp"
#include //muduo的日志
#include
using namespace std;
using namespace muduo;
//获取单例对象的接口函数
ChatService *ChatService::instance()
{
static ChatService service;
return &service;
}
//构造方法,注册消息以及对应的Handler回调操作
ChatService::ChatService()
{
//用户基本业务管理相关事件处理回调注册
_msgHandlerMap.insert({LOGIN_MSG, std::bind(&ChatService::login, this, _1, _2, _3)});
_msgHandlerMap.insert({LOGINOUT_MSG, std::bind(&ChatService::loginout, this, _1, _2, _3)});
_msgHandlerMap.insert({REG_MSG, std::bind(&ChatService::reg, this, _1, _2, _3)});
_msgHandlerMap.insert({ONE_CHAT_MSG, std::bind(&ChatService::oneChat, this, _1, _2, _3)});
_msgHandlerMap.insert({ADD_FRIEND_MSG, std::bind(&ChatService::addFriend, this, _1, _2, _3)});
//群组业务管理相关事件处理回调注册
_msgHandlerMap.insert({CREATE_GROUP_MSG, std::bind(&ChatService::createGroup, this, _1, _2, _3)});
_msgHandlerMap.insert({ADD_GROUP_MSG, std::bind(&ChatService::addGroup, this, _1, _2, _3)});
_msgHandlerMap.insert({GROUP_CHAT_MSG, std::bind(&ChatService::groupChat, this, _1, _2, _3)});
}
//服务器异常,业务重置方法
void ChatService::reset()
{
//把online状态的用户,设置成offline
_userModel.resetState();
}
//获取消息对应的处理器
MsgHandler ChatService::getHandler(int msgid)
{
//记录错误日志,msgid没有对应的事件处理回调
auto it = _msgHandlerMap.find(msgid);
if (it == _msgHandlerMap.end())//找不到
{
//返回一个默认的处理器,空操作,=按值获取
return [=](const TcpConnectionPtr &conn, json &js, Timestamp) {
LOG_ERROR << "msgid:" << msgid << " can not find handler!";//muduo日志会自动输出endl
};
}
else//成功的话
{
return _msgHandlerMap[msgid];//返回这个处理器
}
}
//处理登录业务 id pwd 检测pwd
void ChatService::login(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int id = js["id"].get<int>();//获取id号
string pwd = js["password"];//获取密码
User user = _userModel.query(id);//查找
if (user.getId() == id && user.getPwd() == pwd)//查出来了,登录成功
{
if (user.getState() == "online")
//该用户已经登录,不允许重复登录
{
json response;
response["msgid"] = LOGIN_MSG_ACK;
response["errno"] = 2;
response["errmsg"] = "this account is using, input another!";
conn->send(response.dump());
}
else
{
//登录成功,记录用户连接信息,要考虑线程安全,因为多线程访问
{
lock_guard<mutex> lock(_connMutex);
_userConnMap.insert({id, conn});
}//加个作用域,出了这个右括号就自动解锁
//登录成功,更新用户状态信息 state offline=>online
user.setState("online");
_userModel.updateState(user);
json response;
response["msgid"] = LOGIN_MSG_ACK;
response["errno"] = 0;
response["id"] = user.getId();
response["name"] = user.getName();
//查询该用户是否有离线消息
vector<string> vec = _offlineMsgModel.query(id);//查当用户id
if (!vec.empty())//不为空
{
response["offlinemsg"] = vec;//json库可以和容器之间序列化和反序列化
//读取该用户的离线消息后,把该用户的所有离线消息删除掉
_offlineMsgModel.remove(id);
}
//查询该用户的好友信息并返回
vector<User> userVec = _friendModel.query(id);
if (!userVec.empty())
{
vector<string> vec2;
for (User &user : userVec)
{
json js;
js["id"] = user.getId();
js["name"] = user.getName();
js["state"] = user.getState();
vec2.push_back(js.dump());
}
response["friends"] = vec2;
}
//查询用户的群组信息
vector<Group> groupuserVec = _groupModel.queryGroups(id);
if (!groupuserVec.empty())
{
//group:[{groupid:[xxx, xxx, xxx, xxx]}]
vector<string> groupV;
for (Group &group : groupuserVec)
{
json grpjson;
grpjson["id"] = group.getId();
grpjson["groupname"] = group.getName();
grpjson["groupdesc"] = group.getDesc();
vector<string> userV;
for (GroupUser &user : group.getUsers())
{
json js;
js["id"] = user.getId();
js["name"] = user.getName();
js["state"] = user.getState();
js["role"] = user.getRole();
userV.push_back(js.dump());
}
grpjson["users"] = userV;
groupV.push_back(grpjson.dump());
}
response["groups"] = groupV;
}
conn->send(response.dump());
}
}
else
{
//该用户不存在,用户存在但是密码错误,登录失败
json response;
response["msgid"] = LOGIN_MSG_ACK;
response["errno"] = 1;
response["errmsg"] = "id or password is invalid!";
conn->send(response.dump());
}
}
//处理注册业务 name password
void ChatService::reg(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
string name = js["name"];//获取名字
string pwd = js["password"];//获取密码
User user;//创建用户对象
user.setName(name);
user.setPwd(pwd);
bool state = _userModel.insert(user);//新用户的插入
if (state)//插入成功
{
//注册成功
json response;
response["msgid"] = REG_MSG_ACK;
response["errno"] = 0;
response["id"] = user.getId();
conn->send(response.dump());//回调 ,返回json字符串
}
else//插入失败
{
//注册失败
json response;
response["msgid"] = REG_MSG_ACK;
response["errno"] = 1;
conn->send(response.dump());//回调 ,返回json字符串
}
}
//处理注销业务
void ChatService::loginout(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int userid = js["id"].get<int>();
{
lock_guard<mutex> lock(_connMutex);
auto it = _userConnMap.find(userid);
if (it != _userConnMap.end())
{
_userConnMap.erase(it);
}
}
//更新用户的状态信息
User user(userid, "", "", "offline");
_userModel.updateState(user);
}
//处理客户端异常退出
void ChatService::clientCloseException(const TcpConnectionPtr &conn)
{
User user;
{
lock_guard<mutex> lock(_connMutex);
for (auto it = _userConnMap.begin(); it != _userConnMap.end(); ++it)//用迭代器
{
if (it->second == conn)
{
//从map表删除用户的链接信息
user.setId(it->first);
_userConnMap.erase(it);
break;
}
}
}
//更新用户的状态信息
if (user.getId() != -1)
{
user.setState("offline");
_userModel.updateState(user);
}
}
//一对一聊天业务
void ChatService::oneChat(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int toid = js["toid"].get<int>();//获取对方的id号
{
lock_guard<mutex> lock(_connMutex);//访问连接信息表,要保证线程安全
auto it = _userConnMap.find(toid);//查找对方id号
if (it != _userConnMap.end())//找到了
{
//toid在线,转发消息 服务器主动推送消息给toid用户
it->second->send(js.dump());
return;
}
}
//toid不在线,存储离线消息
_offlineMsgModel.insert(toid, js.dump());
}
//添加好友业务 格式: msgid id friendid
void ChatService::addFriend(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int userid = js["id"].get<int>();//当前用户的id
int friendid = js["friendid"].get<int>();//添加好友的id
//存储好友信息
_friendModel.insert(userid, friendid);
}
//创建群组业务
void ChatService::createGroup(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int userid = js["id"].get<int>();//创建群的用户的id
string name = js["groupname"];
string desc = js["groupdesc"];
//存储新创建的群组信息
Group group(-1, name, desc);
if (_groupModel.createGroup(group))
{
//存储群组创建人信息
_groupModel.addGroup(userid, group.getId(), "creator");
}
}
//加入群组业务
void ChatService::addGroup(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int userid = js["id"].get<int>();
int groupid = js["groupid"].get<int>();
_groupModel.addGroup(userid, groupid, "normal");
}
//群组聊天业务
void ChatService::groupChat(const TcpConnectionPtr &conn, json &js, Timestamp time)
{
int userid = js["id"].get<int>();
int groupid = js["groupid"].get<int>();
vector<int> useridVec = _groupModel.queryGroupUsers(userid, groupid);//查询这个用户所在群组的其他用户id
lock_guard<mutex> lock(_connMutex);//不允许其他人在map里面增删改查
for (int id : useridVec)
{
auto it = _userConnMap.find(id);
if (it != _userConnMap.end())//证明在线,直接转发
{
//转发群消息
it->second->send(js.dump());
}
else//离线
{
//存储离线群消息
_offlineMsgModel.insert(id, js.dump());
}
}
}
我们完善src下的CMakeLists.txt
我们在src下的client下创建文件:CMakeLists.txt
# 定义了一个SRC_LIST变量,包含了该目录下所有的源文件
aux_source_directory(. SRC_LIST)
# 指定生成可执行文件
add_executable(ChatClient ${SRC_LIST})
# 指定可执行文件链接时需要依赖的库文件
target_link_libraries(ChatClient pthread)
因为在客户端有2个线程,一个是读线程,专门接收chatserver服务器发送过来的消息响应,一个是写线程,接收用户的输入,发送相应的业务数据。
基于控制台程序的客户端,没有图形界面,根据我们定义好的命令来请求。
我们在src下的client下创建文件:main.cpp
(不需要高并发,基于linux的TCP客户端编程就可以)
#include "json.hpp"
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using json = nlohmann::json;
#include
#include
#include
#include
#include
#include
#include
#include "group.hpp"
#include "user.hpp"
#include "public.hpp"
//记录当前系统登录的用户信息,记录哪个用户在使用这个聊天客户端
User g_currentUser;
//记录当前登录用户的好友列表信息
vector<User> g_currentUserFriendList;
//记录当前登录用户的群组列表信息
vector<Group> g_currentUserGroupList;
//控制主菜单页面程序
bool isMainMenuRunning = false;
//用于读写线程之间的通信
sem_t rwsem;
//记录登录状态
atomic_bool g_isLoginSuccess{false};
//接收线程 控制台应用程序,接收用户的手动输入,用户不输入cin就阻塞住,所以要2个线程
void readTaskHandler(int clientfd);
//获取系统时间(聊天信息需要添加时间信息)
string getCurrentTime();
//登录成功,才能进入主聊天页面程序
void mainMenu(int);
//显示当前登录成功用户的基本信息
void showCurrentUserData();
//聊天客户端程序实现,main线程用作发送线程,子线程用作接收线程
int main(int argc, char **argv)
{
if (argc < 3)//判断命令的个数,客户端启动要输入命令行的ip地址和端口port
{
cerr << "command invalid! example: ./ChatClient 127.0.0.1 6000" << endl;
exit(-1);
}
//解析通过命令行参数传递的ip和port
char *ip = argv[1];
uint16_t port = atoi(argv[2]);
//创建client端的socket
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == clientfd)
{
cerr << "socket create error" << endl;
exit(-1);
}
//填写client需要连接的server信息ip+port
sockaddr_in server;
memset(&server, 0, sizeof(sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
//client和server进行连接
if (-1 == connect(clientfd, (sockaddr *)&server, sizeof(sockaddr_in)))
{
cerr << "connect server error" << endl;
close(clientfd);//连接失败就关闭描述符
exit(-1);
}
//初始化读写线程通信用的信号量
sem_init(&rwsem, 0, 0);
//连接服务器成功,启动接收子线程
std::thread readTask(readTaskHandler, clientfd); //pthread_create
readTask.detach(); //pthread_detach
//无限循环, main线程用于接收用户输入,负责发送数据
for (;;)
{
//显示首页面菜单 登录、注册、退出
cout << "========================" << endl;
cout << "1. login" << endl;
cout << "2. register" << endl;
cout << "3. quit" << endl;
cout << "========================" << endl;
cout << "choice:";
int choice = 0;
cin >> choice;//输入选择,整数,我们在输入的时候有加回车
cin.get();//读掉缓冲区残留的回车
switch (choice)
{
case 1://login登录业务
{
int id = 0;
char pwd[50] = {0};
cout << "userid:";
cin >> id;
cin.get();//读掉缓冲区残留的回车
cout << "userpassword:";
cin.getline(pwd, 50);
//客户端和服务器要保持语言一致!!!
json js;
js["msgid"] = LOGIN_MSG;
js["id"] = id;
js["password"] = pwd;
string request = js.dump();
g_isLoginSuccess = false;
int len = send(clientfd, request.c_str(), strlen(request.c_str()) + 1, 0);
if (len == -1)
{
cerr << "send login msg error:" << request << endl;
}
sem_wait(&rwsem);//等待信号量,由子线程处理完登录的响应消息后,通知这里
if (g_isLoginSuccess)//登录成功了
{
//进入聊天主菜单页面
isMainMenuRunning = true;
mainMenu(clientfd);
}
}
break;
case 2://register注册业务
{
char name[50] = {0};
char pwd[50] = {0};
cout << "username:";
cin.getline(name, 50);//cin()有回车才结束,遇见空格就结束一个参数的输入
cout << "userpassword:";
cin.getline(pwd, 50);
json js;
js["msgid"] = REG_MSG;
js["name"] = name;
js["password"] = pwd;
string request = js.dump();//json对象序列化成字符串
int len = send(clientfd, request.c_str(), strlen(request.c_str()) + 1, 0);
if (len == -1)//注册失败
{
cerr << "send reg msg error:" << request << endl;
}
sem_wait(&rwsem); //等待信号量,子线程处理完注册消息会通知
}
break;
case 3://quit退出业务,因为还没登录,就退出
close(clientfd);//关闭描述符
sem_destroy(&rwsem);
exit(0);
default://除了1,2,3
cerr << "invalid input!" << endl;
break;
}
}
return 0;
}
//处理注册的响应逻辑
void doRegResponse(json &responsejs)
{
if (0 != responsejs["errno"].get<int>())//注册失败
{
cerr << "name is already exist, register error!" << endl;
}
else//注册成功
{
cout << "name register success, userid is " << responsejs["id"]
<< ", do not forget it!" << endl;
}
}
//处理登录的响应逻辑
void doLoginResponse(json &responsejs)
{
if (0 != responsejs["errno"].get<int>())//登录失败
{
cerr << responsejs["errmsg"] << endl;
g_isLoginSuccess = false;
}
else//登录成功
{
//记录当前用户的id和name
g_currentUser.setId(responsejs["id"].get<int>());
g_currentUser.setName(responsejs["name"]);
//记录当前用户的好友列表信息
if (responsejs.contains("friends"))
{
//初始化
g_currentUserFriendList.clear();
//重新装入,新的登录
vector<string> vec = responsejs["friends"];
for (string &str : vec)
{
json js = json::parse(str);
User user;
user.setId(js["id"].get<int>());
user.setName(js["name"]);
user.setState(js["state"]);
g_currentUserFriendList.push_back(user);
}
}
//记录当前用户的群组列表信息
if (responsejs.contains("groups"))
{
//初始化
g_currentUserGroupList.clear();
vector<string> vec1 = responsejs["groups"];
for (string &groupstr : vec1)
{
json grpjs = json::parse(groupstr);
Group group;
group.setId(grpjs["id"].get<int>());
group.setName(grpjs["groupname"]);
group.setDesc(grpjs["groupdesc"]);
vector<string> vec2 = grpjs["users"];
for (string &userstr : vec2)
{
GroupUser user;
json js = json::parse(userstr);
user.setId(js["id"].get<int>());
user.setName(js["name"]);
user.setState(js["state"]);
user.setRole(js["role"]);
group.getUsers().push_back(user);
}
g_currentUserGroupList.push_back(group);
}
}
//显示登录用户的基本信息
showCurrentUserData();
//显示当前用户的离线消息 个人聊天信息或者群组消息
if (responsejs.contains("offlinemsg"))
{
vector<string> vec = responsejs["offlinemsg"];
for (string &str : vec)
{
json js = json::parse(str);
// time + [id] + name + " said: " + xxx
if (ONE_CHAT_MSG == js["msgid"].get<int>())
{
cout << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
}
else
{
cout << "群消息[" << js["groupid"] << "]:" << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
}
}
}
g_isLoginSuccess = true;
}
}
//登录成功后,启动子线程 - 接收线程
void readTaskHandler(int clientfd)
{
for (;;)
{
char buffer[1024] = {0};
int len = recv(clientfd, buffer, 1024, 0);//阻塞了
if (-1 == len || 0 == len)
{
close(clientfd);
exit(-1);
}
//接收ChatServer转发的数据,反序列化生成json数据对象
json js = json::parse(buffer);
int msgtype = js["msgid"].get<int>();
if (ONE_CHAT_MSG == msgtype)//如果是聊天消息的话
{
cout << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
continue;
}
if (GROUP_CHAT_MSG == msgtype)//如果是群组消息
{
cout << "群消息[" << js["groupid"] << "]:" << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
continue;
}
if (LOGIN_MSG_ACK == msgtype)//登录响应消息
{
doLoginResponse(js);//处理登录响应的业务逻辑
sem_post(&rwsem);//通知主线程,登录结果处理完成
continue;
}
if (REG_MSG_ACK == msgtype)//注册响应消息
{
doRegResponse(js);
sem_post(&rwsem);//通知主线程,注册结果处理完成
continue;
}
}
}
//显示当前登录成功用户的基本信息
void showCurrentUserData()
{
cout << "======================login user======================" << endl;
cout << "current login user => id:" << g_currentUser.getId() << " name:" << g_currentUser.getName() << endl;
cout << "----------------------friend list---------------------" << endl;
if (!g_currentUserFriendList.empty())//如果好友列表不为空
{
for (User &user : g_currentUserFriendList)//打印好友列表id和名字和状态
{
cout << user.getId() << " " << user.getName() << " " << user.getState() << endl;
}
}
cout << "----------------------group list----------------------" << endl;
if (!g_currentUserGroupList.empty())//群组信息不为空,也打印出来
{
for (Group &group : g_currentUserGroupList)
{
cout << group.getId() << " " << group.getName() << " " << group.getDesc() << endl;
for (GroupUser &user : group.getUsers())
{
cout << user.getId() << " " << user.getName() << " " << user.getState()
<< " " << user.getRole() << endl;
}
}
}
cout << "======================================================" << endl;
}
//命令处理方法的声明 ,传递client的fd,用户输入的数据string
//"help" command handler
void help(int fd = 0, string str = "");
//"chat" command handler
void chat(int, string);
//"addfriend" command handler
void addfriend(int, string);
//"creategroup" command handler
void creategroup(int, string);
//"addgroup" command handler
void addgroup(int, string);
//"groupchat" command handler
void groupchat(int, string);
//"loginout" command handler
void loginout(int, string);
//系统支持的客户端命令列表,clientmap
unordered_map<string, string> commandMap = {
{"help", "显示所有支持的命令,格式help"},
{"chat", "一对一聊天,格式chat:friendid:message"},
{"addfriend", "添加好友,格式addfriend:friendid"},
{"creategroup", "创建群组,格式creategroup:groupname:groupdesc"},
{"addgroup", "加入群组,格式addgroup:groupid"},
{"groupchat", "群聊,格式groupchat:groupid:message"},
{"loginout", "注销,格式loginout"}};
//注册系统支持的客户端命令处理,function函数指针函数对象类型 ,如果发现命令就执行相应的方法
unordered_map<string, function<void(int, string)>> commandHandlerMap = {
{"help", help},
{"chat", chat},
{"addfriend", addfriend},
{"creategroup", creategroup},
{"addgroup", addgroup},
{"groupchat", groupchat},
{"loginout", loginout}};
//主聊天页面程序,先显示一下系统支持的命令
void mainMenu(int clientfd)
{
help();//参数有默认值
char buffer[1024] = {0};
while (isMainMenuRunning)//不断的输出
{
cin.getline(buffer, 1024);
string commandbuf(buffer);
string command;//存储命令
int idx = commandbuf.find(":");//找到了返回字符的起始下标
if (-1 == idx)//没找到
{
command = commandbuf;
}
else//找到了
{
command = commandbuf.substr(0, idx);//把命令取出来
}
auto it = commandHandlerMap.find(command);//在maop表找
if (it == commandHandlerMap.end())//找不到
{
cerr << "invalid input command!" << endl;//重新输入
continue;
}
//second命令名字对应的处理函数
//调用相应命令的事件处理回调,mainMenu对修改封闭,添加新功能不需要修改该函数
it->second(clientfd, commandbuf.substr(idx + 1, commandbuf.size() - idx));//调用命令处理方法
}
}
//"help" command handler
void help(int, string)
{
cout << "show command list >>> " << endl;
for (auto &p : commandMap)
{
cout << p.first << " : " << p.second << endl;
}
cout << endl;
}
//"addfriend" command handler
void addfriend(int clientfd, string str)
{
int friendid = atoi(str.c_str());//转为整数
json js;
js["msgid"] = ADD_FRIEND_MSG;
js["id"] = g_currentUser.getId();
js["friendid"] = friendid;
string buffer = js.dump();//序列化
int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send addfriend msg error -> " << buffer << endl;
}
}
//"chat" command handler
void chat(int clientfd, string str)
{
int idx = str.find(":"); // friendid:message
if (-1 == idx)
{
cerr << "chat command invalid!" << endl;
return;
}
int friendid = atoi(str.substr(0, idx).c_str());
string message = str.substr(idx + 1, str.size() - idx);
json js;
js["msgid"] = ONE_CHAT_MSG;
js["id"] = g_currentUser.getId();
js["name"] = g_currentUser.getName();
js["toid"] = friendid;
js["msg"] = message;
js["time"] = getCurrentTime();
string buffer = js.dump();//序列化
int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send chat msg error -> " << buffer << endl;
}
}
//"creategroup" command handler groupname:groupdesc
void creategroup(int clientfd, string str)
{
int idx = str.find(":");
if (-1 == idx)
{
cerr << "creategroup command invalid!" << endl;
return;
}
string groupname = str.substr(0, idx);//获取群名
string groupdesc = str.substr(idx + 1, str.size() - idx);//获取群描述
json js;
js["msgid"] = CREATE_GROUP_MSG;
js["id"] = g_currentUser.getId();
js["groupname"] = groupname;
js["groupdesc"] = groupdesc;
string buffer = js.dump();//json序列化成字符串
int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send creategroup msg error -> " << buffer << endl;
}
}
//"addgroup" command handler
void addgroup(int clientfd, string str)
{
int groupid = atoi(str.c_str());//转成整数
json js;
js["msgid"] = ADD_GROUP_MSG;
js["id"] = g_currentUser.getId();
js["groupid"] = groupid;
string buffer = js.dump();//json序列化成字符串
int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send addgroup msg error -> " << buffer << endl;
}
}
//"groupchat" command handler groupid:message
void groupchat(int clientfd, string str)
{
int idx = str.find(":");
if (-1 == idx)
{
cerr << "groupchat command invalid!" << endl;
return;
}
int groupid = atoi(str.substr(0, idx).c_str());
string message = str.substr(idx + 1, str.size() - idx);
json js;
js["msgid"] = GROUP_CHAT_MSG;
js["id"] = g_currentUser.getId();
js["name"] = g_currentUser.getName();
js["groupid"] = groupid;
js["msg"] = message;
js["time"] = getCurrentTime();
string buffer = js.dump();
int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send groupchat msg error -> " << buffer << endl;
}
}
//"loginout" command handler
void loginout(int clientfd, string)
{
json js;
js["msgid"] = LOGINOUT_MSG;
js["id"] = g_currentUser.getId();
string buffer = js.dump();
int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send loginout msg error -> " << buffer << endl;
}
else
{
isMainMenuRunning = false;
}
}
//获取系统时间(聊天信息需要添加时间信息)
string getCurrentTime()
{
auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
struct tm *ptm = localtime(&tt);
char date[60] = {0};
sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
return std::string(date);
}