转自: http://blog.csdn.net/a19352226/article/details/50850845
Cocos2dx封装了WebSocket,可以直接在项目中使用
废话不多说,直接上代码
[cpp] view plain copy //HelloWorldScene.h #ifndef__HELLOWORLD_SCENE_H__ #define__HELLOWORLD_SCENE_H__ #include"cocos2d.h" #include"cocostudio/CocoStudio.h" #include"ui/CocosGUI.h" #include"network/WebSocket.h"//WebSocket头文件路径 USING_NS_CC; usingnamespacecocostudio::timeline; namespacecocos2d::network;//WebSocket名称空间 namespacecocos2d::ui; classHelloWorld:publiccocos2d::Layer,publicWebSocket::Delegate//WebSocket委托 { public: //there'sno'ID'incpp,sowerecommendreturningtheclassinstancepointer staticcocos2d::Scene*createScene(); //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'ID'incocos2d-iphone virtualboolinit(); //implementthe"staticcreate()"methodmanually CREATE_FUNC(HelloWorld); private: //这些虚函数WebSocket的回调 virtualvoIDonopen(WebSocket*ws); voIDonMessage(WebSocket*ws,153); background-color:inherit; Font-weight:bold">constWebSocket::Data&data); voIDonClose(WebSocket*ws); voIDonError(WebSocket*ws,153); background-color:inherit; Font-weight:bold">constWebSocket::ErrorCode&error); //WebSocket实例化 WebSocket*m_pWebSocket; }; #endif//__HELLOWORLD_SCENE_H__
copy /HelloWorldScene.h.cpp voIDHelloWorld::onopen(WebSocket*ws) { cclOG("Onopen"); } voIDHelloWorld::onMessage(WebSocket*ws,153); background-color:inherit; Font-weight:bold">constWebSocket::Data&data) std::stringtextStr=data.bytes; cclOG(textStr.c_str()); voIDHelloWorld::onClose(WebSocket*ws) if(ws==m_pWebSocket) m_pWebSocket=NulL; } CC_SAFE_DELETE(ws); cclOG("onClose"); voIDHelloWorld::onError(WebSocket*ws,153); background-color:inherit; Font-weight:bold">constWebSocket::ErrorCode&error) charbuf[100]={0}; sprintf(buf,"anerrorwasfired,code:%d",error); cclOG("Errorwasfired,errorcode:%d",108); List-style-type:decimal-leading-zero; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; List-style-position:outsIDe!important"> }
就这么简单! 总结
以上是内存溢出为你收集整理的Cocos2dx WebSocket的使用全部内容,希望文章能够帮你解决Cocos2dx WebSocket的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)