17.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","Arial",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","Arial",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",client->getTag(),data.c_str());
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值