大佬教程收集整理的这篇文章主要介绍了cocos2d-x网络编程三(SocketIO),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
TestSocketIoScene.h文件:
#ifndef __TestSocketIo_SCENE_H__
#define __TestSocketIo_SCENE_H__
#include "cocos2d.h"
#include "network\SocketIO.h"
USING_NS_Cc;
using namespace cocos2d::network;
//继承SocketIO::SIODelegate和实现四个虚函数
class TestSocketIo : public cocos2d::Layer,SocketIO::SIODelegate
{
public:
// there's no 'id' in cpp,so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
virtual bool init();
//socket连接时调用
void onConnect(SIOClient* client);
//收到数据时调用
void onmessage(SIOClient* client,const std::String& data);
//连接错误或接收到错误信号时调用
void onError(SIOClient* client,const std::String& data);
//socket关闭时调用
void onClose(SIOClient* client);
// implement the "static create()" method manually
CREATE_FUNC(TestSocketIo);
SIOClient *client;
};
#endif // __TestSocketIo_SCENE_H__
TestSocketIoScene.cpp文件:
#include "TestSocketIoScene.h"
Scene* TestSocketIo::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = TestSocketIo::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool TestSocketIo::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size size = Director::geTinstance()->getWinSize();
client = nullptr;//初始化为空指针
auto menu = Menu::create();
menu->setPosition(Vec2::ZERO);
addChild(menu);
auto lblInit = Label::create("init socket","Arial",22);
auto menuInit = MenuItemLabel::create(lblInit,[=](Ref *sender){
//1.connect方法创建实例
client = SocketIO::connect("ws://192.168.1.102:3000",*this);
client->setTag("init socket");
//4.初始化的时候设置一个监听器:使用on监听事件和获取接收到的数据
client->on("loginresult",[=](SIOClient *client,const std::String &data){//使用C++匿名函数实现
log("login result is :%s",data.c_str());
});
});
menuInit->setPosition(size/2);
menu->addChild(menuInit);
auto lblSend = Label::create("send message",22);
auto menuSend = MenuItemLabel::create(lblSend,[=](Ref *sender){
//2.send方法发送数据
client->send("Hello socket.io");
});
menuSend->setPosition(size.width/2,size.height/2-50);
menu->addChild(menuSend);
auto lblSendEvent = Label::create("emit event",22);
auto menuSendEvent = MenuItemLabel::create(lblSendEvent,[=](Ref *sender){
//3.向服务器发送login事件,并把名字和密码传给服务器
client->emit("login","[{\"name\":\"myname\",\"pwd\":\"mypwd\"}]");
});
menuSendEvent->setPosition(size.width/2,size.height/2-100);
menu->addChild(menuSendEvent);
return true;
}
void TestSocketIo::onConnect(SIOClient* client){
log("onConnect");
log("%s connect",client->getTag());
}
void TestSocketIo::onmessage(SIOClient* client,const std::String& data){
log("onmessage");
log("%s received content is:%s",client->getTag(),data.c_str());
}
void TestSocketIo::onClose(SIOClient * client){
log("onClose");
log("%s is closed",client->getTag());
}
void TestSocketIo::onError(SIOClient* client,const std::String& data){
log("onError");
log("%s error is:%s",data.c_str());
}
以上是大佬教程为你收集整理的cocos2d-x网络编程三(SocketIO)全部内容,希望文章能够帮你解决cocos2d-x网络编程三(SocketIO)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。