码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x网络通信

时间:2015-06-05 00:11:29      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

Cocos2d-x示例提供了三种内置的网咯通信类

HttpClient,WebSocket,SocketIO。

其中第一个是简单的HTTP协议的使用,提供很多Http请求方式。

剩下的Socket*是针对类Socket连接的方式,不提供应用协议封装,所以测试的时候大部分都是Error出现,但是应该比较适合C/S模式的网络手游开发。

 

HttpClient使用方式简单:

技术分享

 

WebSocket和SocketIO都要实现相应的委托基类中的纯虚函数(类似Java的抽象类的子类实现)

技术分享

 

测试过程中WebSocket总是卡住UI,所以可能有问题。

 

头文件:

 1 #ifndef __NETWORK_SCENE_H__
 2 #define __NETWORK_SCENE_H__
 3 
 4 #include "cocos2d.h"
 5 #include "network/HttpClient.h"
 6 #include "network/HttpRequest.h"
 7 #include "network/HttpResponse.h"
 8 #include "network/SocketIO.h"
 9 #include "network/WebSocket.h"
10 
11 class NetworkScene : public cocos2d::Layer
12     ,public cocos2d::network::SocketIO::SIODelegate
13     , public cocos2d::network::WebSocket::Delegate
14 {
15 public:
16     static cocos2d::Scene* createScene();
17     virtual bool init();
18     void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);
19     CREATE_FUNC(NetworkScene);
20 
21     //SocketIO
22     virtual void onConnect(cocos2d::network::SIOClient* client);
23     virtual void onMessage(cocos2d::network::SIOClient* client, const std::string& data);
24     virtual void onClose(cocos2d::network::SIOClient* client);
25     virtual void onError(cocos2d::network::SIOClient* client, const std::string& data);
26     void testevent(cocos2d::network::SIOClient *client, const std::string& data);
27 
28     //WebSocket,可能处理有误
29     virtual void onOpen(cocos2d::network::WebSocket* ws);
30     virtual void onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data);
31     virtual void onClose(cocos2d::network::WebSocket* ws);
32     virtual void onError(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::ErrorCode& error);
33 
34 
35 private:
36     cocos2d::network::SIOClient *client;
37 };
38 
39 #endif //__NETWORK_SCENE_H__

 

实现cpp

  1 #include "NetworkScene.h"
  2 #include <vector>
  3 #include <string>
  4 
  5 #pragma warning(disable : 4996)
  6 
  7 USING_NS_CC;
  8 
  9 
 10 Scene* NetworkScene::createScene(){
 11     auto scene = Scene::create();
 12     auto layer = NetworkScene::create();
 13     scene->addChild(layer);
 14     return scene;
 15 }
 16 
 17 bool NetworkScene::init(){
 18     if (!Layer::init()){
 19         return false;
 20     }
 21 
 22     auto request = new (std::nothrow)network::HttpRequest();
 23     request->setUrl("http://www.httpbin.org/get");
 24     request->setRequestType(network::HttpRequest::Type::GET);
 25     request->setResponseCallback(CC_CALLBACK_2(NetworkScene::onHttpRequestCompleted,this));
 26     request->setTag("GET TEST1");
 27     network::HttpClient::getInstance()->send(request);
 28     request->release();
 29 
 30     request = new (std::nothrow)network::HttpRequest();
 31     request->setUrl("http://www.httpbin.org/post");
 32     request->setRequestType(network::HttpRequest::Type::POST);
 33     std::vector<std::string> headers;
 34     headers.push_back("Content-Type: application/json;charset=utf-8");
 35     request->setHeaders(headers);
 36     const char *posData = "vistor=cocos2d-x&TestSuite=Extensions Test/NetworkTest";
 37     request->setRequestData(posData, strlen(posData));
 38     request->setTag("POST TEST1");
 39     request->setResponseCallback(CC_CALLBACK_2(NetworkScene::onHttpRequestCompleted,this));
 40     network::HttpClient::getInstance()->send(request);
 41     request->release();
 42 
 43     client = network::SocketIO::connect(*this,"ws://channon.us:300");
 44     client->setTag("SOCKET TEST");
 45     client->on("testEvent", CC_CALLBACK_2(NetworkScene::testevent, this));
 46     //应该收不到消息,因此我关闭的太早了
 47     client->send("SIOCLIENT");
 48     client->disconnect();
 49 
 50 //     auto webSocket = new network::WebSocket();
 51 //     webSocket->init(*this, "ws://echo.websocket.org");
 52 //     if (webSocket->getReadyState() == network::WebSocket::State::OPEN){
 53 //         log("Waiting");
 54 //         webSocket->send("I SEND This Message");
 55 //     }
 56 // 
 57 //     webSocket->close();
 58 
 59     return true;
 60 }
 61 
 62 void NetworkScene::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response){
 63     log("OK");
 64     if (0 != strlen(response->getHttpRequest()->getTag())){
 65         log("%s completed ", response->getHttpRequest()->getTag());
 66 
 67         int responseCode = response->getResponseCode();
 68         log("%d", responseCode);
 69 
 70         if (response->isSucceed()){
 71             log("Successed");
 72         }
 73 
 74         std::vector<char> *buffer = response->getResponseData();
 75         std::string buf(buffer->begin(), buffer->end());
 76         log("%s", buf.c_str());
 77     }
 78 }
 79 
 80 void NetworkScene::testevent(cocos2d::network::SIOClient *client, const std::string& data){
 81     log("OK %s ",data.c_str());
 82 }
 83 
 84 void NetworkScene::onConnect(network::SIOClient* client)
 85 {
 86     log("SocketIOTest::onConnect called");
 87 }
 88 
 89 void NetworkScene::onMessage(network::SIOClient* client, const std::string& data)
 90 {
 91     log("SocketIOTest::onMessage received: %s", data.c_str());
 92 }
 93 
 94 void NetworkScene::onClose(network::SIOClient* client)
 95 {
 96     log("SocketIOTest::onClose called");
 97 }
 98 
 99 void NetworkScene::onError(network::SIOClient* client, const std::string& data)
100 {
101     log("SocketIOTest::onError received: %s", data.c_str());
102 }
103 
104 
105 
106 void NetworkScene::onOpen(cocos2d::network::WebSocket* ws){
107     log("open %p", ws);
108 }
109 void NetworkScene::onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data){
110     log("message %p", ws);
111 }
112 void NetworkScene::onClose(cocos2d::network::WebSocket* ws){
113     log("close %p", ws);
114 }
115 void NetworkScene::onError(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::ErrorCode& error){
116     log("error %p", ws);
117 }

 

测试结果就无法贴出来,特别是后面的两个Socket。

 

以上内容仅供参考,代码很有可能有重大BUG。

 

以上。

Cocos2d-x网络通信

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4553272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!