Have previously shared a framework based on COCOS2DX and the server using the socket to communicate, but also unfamiliar to the following blog post:
"C/S communication interaction Socket" COCOS2DX (Client) use BSD socket and Mina (Server) mobile games Communication Framework!
So today Himi to share how to use HTTP in cocos2dx to access the server side and get the data;
Here for the server side, Himi selection, Jetty, Jetty is not familiar with the first self-baidu~google~ is a servlet container. Similar to JSP. What is a servlet? Jsp? = =. Don't dwell on it. Everybody manual good;
Below we simply write a server side (how to create a Jetty server see Himi Jetty Development Series article)
——— – First server-side ———— –
This is where IBU writes the jar package build path that created the project and configuration item. Directly on the main code snippet:
The first is the jetty server main class: (Here the Himi IDE is 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();
}
}
|
Then there is one of our servlet classes:
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)
throws
ServletException, 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);
}
}
}
|
In our servlet, we get back the data from the HTTP clinet end. Middle Simple to the client some simple string ~
OK, start our jetty server, right-Servletserver.java run, watch the console:
12 |
2012-05-25 16:43:04.767:INFO:oejs.Server:jetty-8.1.3.v20120416 2012-05-25 16:43:05.110:INFO:oejs.AbstractConnector:Started [email protected]:8080 |
Appears as above, indicating that your jetty server started successfully; OK. Then design the client code;
——— – then Cocos2dx clinet End ———— –
Start by creating a new COCOS2DX project, which doesn't say much. Then replace the following code in the default HelloWorldScene.cpp initialization function:
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, then we import the curl.h header file:
Not yet, this time prompted us to find this header file, OK, continue to operate two steps as follows:
1. Add the Libcurl.a file: (This file defaults to the Cocos2dx/platform/third_party/ios/libraries folder under the COCOS2DX engine package)
Don't worry at this time will also prompt the header file can not find;
Click your COCOS2DX project in Xcode, then select the targets of your project and find the Search Paths in Build settings:
Double-click your Library Search Paths to observe:
The following "$...../third_party/ios/libraries" path is added by default when you add Lib Curl.a in the first step. We don't want to change this, but please double-click the path and copy it.
What we need to modify is the previous property of this property, Header Search Paths;
Double-click the header Search paths the connection after the property, and then click on the "+" sign to add a path, this path is just your copy of the path, but after pasting also set this path to the path of the previous folder, so a bit around, in fact, is as follows:
Assuming that your previous copy path is "$...../third_party/ios/libraries"
So when you paste here, the path should be: "$...../third_party/ios"
Ok,himi The path here also gives you a convenient comparison:
OK, if the above steps are normal then compile will have no problem;
After the compilation succeeds, Command+r runs the project, observes the Xcode console print, and the server-side printing: Normally it should be as follows:
OK, everything is fine.
Note: The new project with the script, the default is no Libcurl, you compile to the other platform when you want to modify the makefile file to add it, (specifically, you can refer to tests inside the makefile)
Reminder: Here the client and the server is just a simple HTTP interaction, no more details of processing, such as client side access should be another thread, the interaction of data to have a certain protocol specification and so on when the introduction of the socket has been said, here is not much to say;
http://blog.csdn.net/linking530/article/details/39401155
"HTTP text of c/s communication interaction" COCOS2DX (Client) uses Curl and jetty (Server) to implement the HTTP communication framework for mobile online games (contains the solution curl.h header file can not find the problem)