---
#1.web客户端代码分析
web客户端传4个值,userID,passWord,IPAddr,IPPort,最后2个是连接服务器用的,userID和passWord是验证用户信息用的。
以下是客户端代码:
var userID = $('#userID').val();
var passWord = $('#passWord').val();
var IpAddr = $('#IpAddr').val();
var IpPort = $('#IpPort').val();
var host = "ws://"+IpAddr+":"+IpPort+"/ws"; //host地址,用来连接服务器
var jsonData = {};
jsonData.MainCmdID = 1;
jsonData.SubCmdID = 2;
jsonData.userID = 1;
jsonData.passWord = hex_md5(passWord); //对密码进行md5加密处理
toStr = JSON.stringify(jsonData); //把数据转换为json格式
OutputLog(toStr);
try{
socket = new WebSocket(host); //连服务器
OutputLog('Socket Status: '+socket.readyState);
socket.onopen = function(){
var myData = new Date(); //获取当前时间信息
OutputLog(myData.toLocaleString()+'Socket Status: '+socket.readyState+'(open)');
socket.send(toStr); //向服务器发送消息
}
socket.onmessage = function(msg){ //收到服务器消息
var myData = new Date();
var jsonData = JSON.stringify(msg.data);
var jsonObj = window.JSON.parse(jsonData);
OutputLog(myData.toLocaleString()+":"+jsonObj);//输出服务器消息
}
socket.onclose = function(){//接收服务器关闭消息
var myData = new Date();
OutputLog(myData.toLocaleString()+'Socket Status: '+socket.readyState+' (Close)');
}
}catch(exception){//异常处理
OutputLog('Error' + exception);
}
---
#2.服务器处理web客户端请求
线程CServerSocketRSThread::OnEventThreadRun() 不停循环执行,
CServerSocketItem::OnRecvCompleted()函数中的recv捕获客户端信息事件。
根据:
//接收数据
int iRetCode = recv(m_hSocket, (char *)m_cbRecvBuf + m_wRecvSize, sizeof(m_cbRecvBuf)-m_wRecvSize, 0);
if (iRetCode <= 0)
{
CloseSocket(m_wRountID);
return true;
}
//TODO 根据连接类型来做接受数据后的处理
//连接类型检测
if (this->m_connectType == ConnectType_unknow){
string s((char *)m_cbRecvBuf);
if (s.find("HTTP") != string::npos){
this->m_connectType = ConnectType_websocket;
}
else{
this->m_connectType = ConnectType_socket;
}
}
判断是否是web信息,然后发送给子项去处理具体消息。
然后在CAttemperEngineSink::OnEventTCPNetworkRead()函数中捕获网络事件,
增加函数:
///< qsz 解析网络登录包
if (Command.wMainCmdID == MDM_WEB_MSG)
{
OutputDebugString((char *)pData);
char* resulData = new char[wDataSize + 1];
memcpy(resulData, pData, wDataSize);
resulData[wDataSize] = '\0';
std::string stResult = resulData;
Json::Reader readerJson;
Json::Value valueJson;
if (readerJson.parse(stResult, valueJson))
{
Command.wMainCmdID = valueJson["MainCmdID"].asInt();
Command.wSubCmdID = valueJson["SubCmdID"].asInt();
//
MainCmdID = 11, SubCmdID = 222, field1=11,field2=222
}
else
return false;
if (Command.wMainCmdID == MDM_GR_LOGON)
{
int userID = valueJson["userID"].asInt();
std::string passWord = valueJson["passWord"].asString();
///< 客户端上传时已转成md5
CMD_GR_LogonByUserID * pLogonByUserID = new CMD_GR_LogonByUserID;
pLogonByUserID->dwPlazaVersion = VER_PLAZA_FRAME;
pLogonByUserID->dwUserID = userID;
lstrcpyn(pLogonByUserID->szPassWord, passWord.c_str(), passWord.length() + 1);
pData = pLogonByUserID;
}
解析处理函数消息。
最后增加自己的函数处理返回消息,并发送给客户端。
void CAttemperEngineSink::SendDataEx(DWORD dwSocketID, WORD wMainCmdID, WORD wSubCmdID, void * pData, WORD wDataSize){
int connectType = m_pITCPNetworkEngine->getConnectType(dwSocketID);
if (connectType == 2){//ConnectType_websocket
//TODO tmx Data2WebData处理
Json::Value root;
Json::Value arrayObj;
Json::Value item;
item["MainCmdID"] = 1;
item["SubCmdID"] = 1;
arrayObj.append(item);
root["array"] = arrayObj;
root.toStyledString();
std::string out = root.toStyledString();
OutputDebugString("======CAttemperEngineSink::SendDataEx======\n");
OutputDebugString(out.c_str());
m_pITCPNetworkEngine->SendData(dwSocketID, wMainCmdID, wSubCmdID, (void*)out.c_str(), out.length() );
item.clear();
arrayObj.clear();
root.clear();
}
else{
if (wDataSize == 0)
{
m_pITCPNetworkEngine->SendData(dwSocketID, wMainCmdID, wSubCmdID);
}
else{
m_pITCPNetworkEngine->SendData(dwSocketID, wMainCmdID, wSubCmdID, pData, wDataSize);
}
}
}
至此,web客户端便能登录游戏大厅,并在其它客户端上显示到此玩家信息了!
---