写在前面:这里再把下行数据这部分加上
一.handle函数的实现,如下:
#include "JsonCpp/json.h"
#include "DownstreamHandler.h"
void DownstreamHandler::handle(ClientSession* client, const char* pack, int length){
Json::Value v;
Json::Reader reader;
if(reader.parse(pack, v)){
int uri = v["uri"].asUInt();
std::string msg = v["msg"].asString();
#ifdef OPEN_LOG
printf("[DownstreamHandler::handle] uri is %d\n", uri);
printf("[DownstreamHandler::handle] msg is %s\n", msg.c_str());
#endif
switch(uri){
case PLoginRes:
onLoginRes(client, pack, length);
break;
case PPingRes:
onPingRes(client, pack, length);
break;
default:
printf("[DownstreamHandler::handle] Unknown URI:%d\n", uri);
break;
}
}
}
二.登录信息响应函数的实现,如下:
void DownstreamHandler::onLoginRes(ClientSession* client, const char* pack, int length){
#ifdef OPEN_LOG
printf("[DownstreamHandler::onLoginRes] ~~~~~~~~~~ invoke ~~~~~~~~~\n");
#endif
Json::Value jv;
Json::FastWriter writer;
int uri = 3;
std::string msg = "ustc";
jv["uri"] = Json::Value(uri);
jv["msg"] = Json::Value(msg);
std::string loginRes = writer.write(jv);
client->sendData(loginRes);
}
说明:Json是一个开源库,即以json的格式返回响应信息
三.ping心跳信息响应函数的实现,如下:
void DownstreamHandler::onPingRes(ClientSession* client, const char* pack, int length){
#ifdef OPEN_LOG
printf("[DownstreamHandler::onPingRes] ~~~~~~~~~~ invoke ~~~~~~~~~\n");
#endif
Json::Value jv;
Json::FastWriter writer;
int uri = 5;
std::string msg = "linyanwen";
jv["uri"] = Json::Value(uri);
jv["msg"] = Json::Value(msg);
std::string pingRes = writer.write(jv);
client->sendData(pingRes);
}
说明:Json是一个开源库,即以json的格式返回响应信息
~未完待续
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8315883