转自: http://blog.csdn.net/a19352226/article/details/50850845
Cocos2dx封装了WebSocket,可以直接在项目中使用
废话不多说,直接上代码
copy
//HelloWorldScene.h.cppinit函数
m_pWebSocket=newWebSocket();
m_pWebSocket->init(*this,"ws://localhost:1234");//实例化WebSocket并连接
copy
/HelloWorldScene.h.cpp
void HelloWorld::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"> }
就这么简单! 原文链接:https://www.f2er.com/cocos2dx/338726.html