生活随笔
收集整理的這篇文章主要介紹了
【玩转cocos2d-x之三十一】弱联网与服务器的通讯
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原創作品,轉載請標明:http://blog.csdn.net/jackystudio/article/details/17347069
這里采用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
用$_POST獲取表單數據,表單數據則是不可見的,比如
http://www.w3school.com.cn/welcome.php
詳細可見http://www.w3school.com.cn/php
3.服務器php處理代碼
這里我直接修改了主頁index.html。會C++應該都能看懂,先是打開一個log.txt,接收到username和password,如果是username是jackystudio,password是123的話,把username和password寫入log.txt,并返回登錄成功,如果username或password錯誤時返回登錄失敗。如果未接收到則返回沒有用戶名密碼。
3.1.采用get方式代碼
[php]?view plaincopy
<html>?? <body>?? <?php?? $open=fopen("log.txt","a"?);??? if(isset($_GET["username"])?&&?isset($_GET["password"]))?? {?? if($_GET["username"]=="jackystudio"?&&?$_GET["password"]=="123")?? {?? fwrite($open,"Username:".$_GET["username"]);?? fwrite($open,"\r\n");?? fwrite($open,"Password:".$_GET["password"]);?? echo?"Login?Success";??? }?? else?? {?? fwrite($open,"Wrong?Username?or?password!");?? echo?"Login?Failed";??? }?? }?? else?? {?? fwrite($open,"No?password");?? echo?"No?Username?or?Password";??? }?? fclose($open);?? ?>?? </body>?? </html>??
3.2.采用post方式代碼
[php]?view plaincopy
<html>?? <body>?? <?php?? $open=fopen("log.txt","a"?);??? if(isset($_POST["username"])?&&?isset($_POST["password"]))?? {?? if($_POST["username"]=="jackystudio"?&&?$_POST["password"]=="123")?? {?? fwrite($open,"Username:".$_POST["username"]);?? fwrite($open,"\r\n");?? fwrite($open,"Password:".$_POST["password"]);?? echo?"Login?Success";??? }?? else?? {?? fwrite($open,"Wrong?Username?or?password!");?? echo?"Login?Failed";??? }?? }?? else?? {?? fwrite($open,"No?password");?? echo?"No?Username?or?Password";??? }?? fclose($open);?? ?>?? </body>?? </html>??
4.cocos2d-x使用CCHttpClient類進行網絡請求
CCHttpClient的使用這里也不贅述了,請移步官方文檔How_to_use_CCHttpClient。這里在上文編輯框和點九圖的基礎上進行了修改。2個編輯框,分別是username和password。一個按鈕點擊發送請求。一個文本顯示從服務器返回的結果。
4.1.按鈕請求處理
[cpp]?view plaincopy
void?TestLayer::btncallback(?CCObject*?pSender?)?? {?? ????bool?requestType_is_get=true;?? ????if?(requestType_is_get)?? ????{?? ????????CCHttpRequest*?request?=?new?CCHttpRequest();?? ????????string?str1?=?"127.0.0.1:80/index.html?";?? ????????string?str2?=?p_User_EditBox->getText();?? ????????string?str3?=?p_Psw_EditBox->getText();?? ????????string?struser="username=";?? ????????string?strpsw="&password=";?? ????????str1=str1+struser+str2+strpsw+str3;?? ????????request->setUrl(str1.c_str());?? ????????request->setRequestType(CCHttpRequest::kHttpGet);?? ????????request->setResponseCallback(this,?httpresponse_selector(TestLayer::onHttpRequestCompleted));?? ????????request->setTag("GET?test");?? ????????CCHttpClient::getInstance()->send(request);?? ????????request->release();?? ????}?? ????else?? ????{?? ????????CCHttpRequest*?request?=?new?CCHttpRequest();?? ????????string?str1?=?"127.0.0.1:80/index.html";?? ????????string?str2?=?p_User_EditBox->getText();?? ????????string?str3?=?p_Psw_EditBox->getText();?? ????????string?struser="username=";?? ????????string?strpsw="&password=";?? ????????str2=struser+str2+strpsw+str3;?? ?? ????????request->setUrl(str1.c_str());?? ????????request->setRequestType(CCHttpRequest::kHttpPost);?? ????????request->setResponseCallback(this,?httpresponse_selector(TestLayer::onHttpRequestCompleted));?? ?? ????????const?char*?postData?=?str2.c_str();?? ????????request->setRequestData(postData,?strlen(postData));?? ?????????? ????????request->setTag("POST?test");?? ????????CCHttpClient::getInstance()->send(request);?? ????????request->release();?? ????}?? }??
4.2.響應回調處理
[cpp]?view plaincopy
void?TestLayer::onHttpRequestCompleted(?CCHttpClient*?client,?CCHttpResponse*?response?)?? {?? ????if?(!response->isSucceed())?? ????{???? ????????CCString?strError;?? ????????strError.initWithFormat("Receive?Error!?\n%s\n",response->getErrorBuffer());?? ????????m_labelStatusCode->setString(strError.getCString());?? ????????return?;????? ????}???? ?? ????std::vector<char>?*buffer?=?response->getResponseData();?? ????string?recieveData;?? ????for?(unsigned?int?i?=?0;?i?<?buffer->size();?i++)?? ????{???? ????????recieveData?+=?(*buffer)[i];???? ????}?? ????size_t?begin=?recieveData.find("<body>")+6;?? ????size_t?end=?recieveData.find("</body>");?? ????string?result(recieveData,begin,end-begin);?? ????m_labelStatusCode->setString(result.c_str());?? }??
5.效果圖
5.1.Apache運行(Get和Post兩種效果都是一樣的)
(1)帳號密碼正確時
(2)帳號密碼錯誤時
5.2.關閉Apache
6.源碼下載
下載地址:http://download.csdn.net/detail/jackyvincefu/6713471
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的【玩转cocos2d-x之三十一】弱联网与服务器的通讯的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。