原创作品,转载请标明:http://www.jb51.cc/article/p-ndohzuqc-ep.html
这里采用Apache+PHP搭建了一个简易服务器,服务端用PHP语言,客户端采用cocos2d-x的CCHttpClient类通过http方式访问服务端资源。模拟了cocos2d-x提交账户和密码到服务端,服务端校验帐号密码,如果正确返回客户端成功登录,如果错误则返回错误信息,同时在服务端后台保存登录log。第一次接触PHP,语法上和C/C++还是蛮像的,主要是给出一个cocos2d-x网络实例,代码中并没有做一些防呆纠错措施。
1.搭建Apache+PHP网页服务器
Apche2.2 x86版下载地址:http://pan.baidu.com/s/1vNuLF
PHP5.2.17版下载地址:http://pan.baidu.com/s/17sFoN
搭建过程参见http://tech.163.com/06/0206/11/299AMBLT0009159K.html,这里就不安装MysqL了。
搭建成功后,打开http://127.0.0.1,就可以看到"It' works!"字样。同时打开Apache monitor监控Apache处于运行状态。我这里使用的80端口。
2.PHP收集表单的方式
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE,对应着查改增删,这里介绍GET和POST。
用$_GET获取表单数据,表单数据对任何人都是可见的,比如
http://www.w3school.com.cn/welcome.PHP?username=jackystudio&password=123
@H_301_67@
http://www.w3school.com.cn/welcome.PHP
@H_301_67@
@H_301_67@
3.服务器PHP处理代码
@H_301_67@
@H_301_67@
3.1.采用get方式代码
- <html>
- <body>
- <?PHP
- $open=fopen("log.txt","a");//Savepassword
- if(isset($_GET["username"])&&isset($_GET["password"]))
- {
- if($_GET["username"]=="jackystudio"&&$_GET["password"]=="123")
- fwrite($open,"Username:".$_GET["username"]);
- fwrite("\r\n");
- "Password:".$_GET["password"]);
- echo"LoginSuccess";//returntoclient
- }
- else
- {
- "WrongUsernameorpassword!");
- echo"LoginFailed";//returntoclient
- }
- "Nopassword");
- echo"NoUsernameorPassword"; fclose($open);
- ?>
- </body>
- </html>
@H_301_67@
3.2.采用post方式代码
- <html>
- <body>
- <?PHP
- //Savepassword @H_638_403@$_POST["username"])&&isset($_POST["password"]))
- { @H_638_403@$_POST["username"]=="jackystudio"&&$_POST["password"]=="123")
- $_POST["username"]);
- "\r\n"); @H_638_403@$_POST["password"]);
- //returntoclient
- }
- else
- {
- "WrongUsernameorpassword!"); @H_638_403@//returntoclient
- }
- "Nopassword"); @H_638_403@$open);
- ?>
- </body>
- </html>
4.cocos2d-x使用CCHttpClient类进行网络请求
CCHttpClient的使用这里也不赘述了,请移步官方文档How_to_use_CCHttpClient。这里在上文编辑框和点九图的基础上进行了修改。2个编辑框,分别是username和password。一个按钮点击发送请求。一个文本显示从服务器返回的结果。
4.1.按钮请求处理
- voidTestLayer::btncallback(CCObject*pSender)
- boolrequestType_is_get=true;//采用get方式或者post方式
- if(requestType_is_get)
- CCHttpRequest*request=newCCHttpRequest();//创建请求对象
- stringstr1="127.0.0.1:80/index.html?";
- stringstr2=p_User_EditBox->getText();//获取username编辑框内容
- stringstr3=p_Psw_EditBox->getText();//获取password编辑框内容
- stringstruser="username=";
- stringstrpsw="&password=";
- str1=str1+struser+str2+strpsw+str3;
- request->setUrl(str1.c_str());//设置请求的url,username和password已经包含在url中
- request->setRequestType(CCHttpRequest::kHttpGet);//设置为Get模式
- request->setResponseCallback(this,httpresponse_selector(TestLayer::onHttpRequestCompleted));//设置响应的回调
- request->setTag("GETtest");
- CCHttpClient::getInstance()->send(request);//发送请求
- request->release();//释放请求
- stringstr1="127.0.0.1:80/index.html";
- stringstr2=p_User_EditBox->getText();
- stringstr3=p_Psw_EditBox->getText();
- str2=struser+str2+strpsw+str3;
- request->setUrl(str1.c_str());//设置请求的url,只是请求页面的url,并不包含username和password
- request->setRequestType(CCHttpRequest::kHttpPost);//设置为Post模式
- request->setResponseCallback(//设置响应的回调
- constchar*postData=str2.c_str();
- request->setRequestData(postData,strlen(postData));//设置请求数据,也就是username和password
- request->setTag("POSTtest");
- CCHttpClient::getInstance()->send(request);//发送请求
- request->release();//释放请求
- }
4.2.响应回调处理
- voidTestLayer::onHttpRequestCompleted(CCHttpClient*client,CCHttpResponse*response)
- if(!response->isSucceed())//如果响应失败,输出错误信息
- CCStringstrError;
- strError.initWithFormat("ReceiveError!\n%s\n",response->getErrorBuffer());
- m_labelStatusCode->setString(strError.getCString());
- return;
- std::vector<char>*buffer=response->getResponseData();//接收响应信息
- stringrecieveData;
- for(unsignedinti=0;i<buffer->size();i++)
- recieveData+=(*buffer)[i];
- size_tbegin=recieveData.find("<body>")+6;//这里简单处理,获取<body>标签内数据,即是响应内容
- size_tend=recieveData.find("</body>");
- stringresult(recieveData,begin,end-begin);
- m_labelStatusCode->setString(result.c_str());
- }
5.效果图
5.1.Apache运行(Get和Post两种效果都是一样的)
(1)帐号密码正确时
(2)帐号密码错误时
5.2.关闭Apache