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 instancebool 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)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)