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());
}

相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...