【C/S通訊互動之Http篇】Cocos2dx(Client)使用Curl與Jetty(Server)實現手機網遊Http通訊架構(內含解決curl.h標頭檔找不到問題)

來源:互聯網
上載者:User

標籤:開發   log   uil   訪問   efi   sdn   eth   網遊   session   

之前已經分享過一篇基於Cocos2dx與伺服器使用Socket進行通訊的架構,還不太熟悉的請移步到如下博文中:

【C/S通訊互動之Socket篇】Cocos2dx(Client)使用BSD Socket與Mina(Server)手機網遊通訊架構!

那麼今天Himi來分享如何在cocos2dx中使用Http來訪問Server端並且擷取資料;

這裡對於Server端,Himi選用,Jetty,對於Jetty不太熟悉的可以先自行baidu~google~是個servlet的容器。類似JSP。 什麼是servlet?jsp? = =。不贅述了。大家手動好吧;

下面我們簡單書寫一個Server端(如何建立一個Jetty伺服器請看Himi  Jetty 開發系列文章)

———–首先伺服器端————–

這裡就ibu寫建立項目和設定項目的jar包 build path了。直接上主要程式碼片段:

       首先是Jetty  Server主類:(這裡Himi用的IDE 是 Eclipse)

1234567891011121314151617181920212223242526 ServletServer.java import org.eclipse.jetty.server.Server;import org.eclipse.jetty.servlet.ServletContextHandler;import org.eclipse.jetty.servlet.ServletHolder; import servlet.HServlet; /** * @author Himi */public class ServletServer {    public static void main(String[] args) throws Exception {        Server server = new Server(8080);          ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);        context.setContextPath("/");          server.setHandler(context);          context.addServlet(new ServletHolder(new HServlet()), "/himi");          server.start();        server.join();    }}

然後是我們的一個Servlet類:

12345678910111213141516171819202122232425262728293031323334353637383940414243 HServlet.java package servlet; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; /** * @author Himi */public class HServlet extends HttpServlet {    private static final long serialVersionUID = 1L;      public HServlet() {         protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {        System.out.println("~~~~有一個Clinet訪問!~~~~");         //擷取http Client端對應的兩個欄位的資料        String name = request.getParameter("name");        String password = request.getParameter("password");        //設定字元編碼        response.setCharacterEncoding("UTF-8");        response.setContentType("text/html");        response.setStatus(HttpServletResponse.SC_OK);        response.getWriter().println("Server say: 測試中文:session=" + request.getSession(true).getId());          if(name!=null) {            response.getWriter().println("Server say:名字:"+name);            System.out.println("Client say: name="+name);        }         if(password!=null) {            response.getWriter().println("Server say:密碼:"+password);            System.out.println("Client say: password="+password);        }    }}

我們的Servlet裡,就是得到http clinet端傳過來的資料返回回去。中間簡單寫給用戶端一些簡單字串~

      OK,啟動我們的Jetty伺服器,右鍵ServletServer.Java run,觀察控制台:

12 2012-05-25 16:43:04.767:INFO:oejs.Server:jetty-8.1.3.v201204162012-05-25 16:43:05.110:INFO:oejs.AbstractConnector:Started [email protected]:8080

出現如上,表示你的Jetty Server啟動成功;OK。然後設計用戶端代碼;

 

———–然後cocos2dx Clinet端————–

首先建立一個cocos2dx項目,這個不多說。然後在預設的HelloWorldScene.cpp 初始化函數替換如下代碼:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 bool HelloWorld::init(){ /* *@author By Himi */     //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }    CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World""Thonburi", 34);    CCSize size = CCDirector::sharedDirector()->getWinSize();    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );    this->addChild(pLabel, 1);     CURL *curl;    CURLcode res;    char buffer[10];     curl = curl_easy_init();    if (curl)    {//        curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8080/himi");        curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8080/himi?name=xiaoming&password=李華明");        res = curl_easy_perform(curl);        /* always cleanup */        curl_easy_cleanup(curl);        if (res == 0)        {            pLabel->setString("0 response");        }        else        {            sprintf(buffer,"code: %i",res);            pLabel->setString(buffer);        }    }    else    {        pLabel->setString("no curl");    }     return true;}

OK,然後我們匯入 curl.h標頭檔:

1 #include "curl/curl.h"

還沒完,這時候提示我們找不到這個標頭檔,OK,繼續操作兩步如下:

1.   加入 libcurl.a 檔案:(此檔案預設在cocos2dx引擎包下的cocos2dx/platform/third_party/iOS/libraries檔案夾下)

別著急這時候還會提示標頭檔找不到;

在xcode中點擊你的cocos2dx項目,然後選擇你項目的 targets,然後在Build Settings中找到 Search Paths:

雙擊你的 Library Search Paths 觀察:如:

 

下面那個”$…../third_party/ios/libraries”路徑是你第一步添加lib curl.a的時候預設添加的。這個我們不要修改;但是請雙擊這個路徑然後copy下來;

我們需要修改的是此屬性的上一個屬性,Header Search Paths;

雙擊Header Search Paths屬性後面的串連,然後點擊“+”號添加一個路徑,這個路徑就是剛才你copy的路徑,但是粘貼後還要將此路徑設定到上一個檔案夾的路徑;這麼說有點繞,其實就是如下:

假設你之前copy的路徑是  “$…../third_party/ios/libraries”

   那麼你在這裡粘貼的時候路徑應該是: “$…../third_party/ios”

OK,Himi這裡的路徑也給大家一張便於對比:

 

OK,如果以上步驟都操作正常那麼編譯將沒有任何問題;

編譯成功後,command+R運行項目,觀察xcode控制台列印,以及伺服器端列印:正常情況下應該如下:

 

OK,一切正常;

       注意:用指令碼建立的工程,預設是不加libcurl的,大家編譯到其他平台的時候要修改makefile檔案將其添加進去;(具體可以參考tests裡面的makefile ) 

       提醒 :這裡用戶端與伺服器只是簡單的http互動,沒有更細節的處理,例如 Client端訪問應該另起一個線程,互動的時候資料要有一定的協議規範等等這些在介紹Socket的時候都有說過了,這裡就不多說了;

 

http://blog.csdn.net/linking530/article/details/39401155

【C/S通訊互動之Http篇】Cocos2dx(Client)使用Curl與Jetty(Server)實現手機網遊Http通訊架構(內含解決curl.h標頭檔找不到問題)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.