先给大家看下example
client = new NodejsClient();
client->open("ws://www.xxxxxxxxx.com:8100");
client->addListener(NodejsClient::OPEN, CC_CALLBACK_1(Game::onOpen, this));//侦听开始
client->addListener(NodejsClient::CLOSE, CC_CALLBACK_1(Game::onClose, this));//侦听关闭
client->addListener(NodejsClient::MESSGAE, CC_CALLBACK_1(Game::onMessage, this));//侦听数据传输
client->addListener(NodejsClient::ERR, CC_CALLBACK_1(Game::onError, this));//侦听错误
client->send("msg:100");
client->close();
主要是服务器发送信息时候的侦听
void Game::onMessage(cocos2d::EventCustom* e){
std::string str = *(std::string*)e->getUserData();
CCLOG("messge from server, msg:%s", str.c_str());
}
这是头文件
#ifndef __NODE_JS_CLIENT_H__
#define __NODE_JS_CLIENT_H__
#include "cocos2d.h"
#include "network/WebSocket.h"
class NodejsClient : cocos2d::network::WebSocket::Delegate
{
public:
NodejsClient();
virtual ~NodejsClient();
/**
* 打开连接
*/
void open(std::string address);
/**
* 关闭连接
*/
void close();
/**
* 发送信息
*/
void send(std::string msg);
/**
* 接收原本的数据
*/
virtual void onOpen(cocos2d::network::WebSocket* ws);
virtual void onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data);
virtual void onClose(cocos2d::network::WebSocket* ws);
virtual void onError(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::ErrorCode& error);
/**
* 侦听
*/
void addListener(const std::string &eventName, const std::function<void(cocos2d::EventCustom*)>& callback);
/**
* 数据连接成功
*/
static const std::string OPEN;
/**
* 接到服务器数据
* @data std::string str 服务器传输的字符串
*/
static const std::string MESSGAE;
/**
* 连接关闭
*/
static const std::string CLOSE;
/**
* 错误
* @data int code 错误代码
*/
static const std::string ERR;
private:
/*
* 客户端
*/
cocos2d::network::WebSocket* _client;
/**
* 事件
*/
cocos2d::EventDispatcher* _evt;
};
#endif
下面是cpp
#include "game/NodejsClient.h"
USING_NS_CC;
const std::string NodejsClient::OPEN = "nodejsClientOpen";
const std::string NodejsClient::MESSGAE = "nodejsClientMessage";
const std::string NodejsClient::CLOSE = "nodejsClientClose";
const std::string NodejsClient::ERR = "nodejsClientError";
NodejsClient::NodejsClient(){
_evt = new EventDispatcher();
_evt->setEnabled(true);
_client = new network::WebSocket();
}
NodejsClient::~NodejsClient(){
}
void NodejsClient::open(std::string address){
_client->init(*this, address);
}
void NodejsClient::close(){
_client->close();
}
void NodejsClient::addListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback){
_evt->addCustomEventListener(eventName, callback);
}
void NodejsClient::send(std::string msg){
_client->send(msg);
}
void NodejsClient::onOpen(cocos2d::network::WebSocket* ws)
{
_evt->dispatchCustomEvent(NodejsClient::OPEN, nullptr);
}
void NodejsClient::onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data)
{
std::string str = data.bytes;
_evt->dispatchCustomEvent(NodejsClient::MESSGAE, &str);
}
void NodejsClient::onClose(cocos2d::network::WebSocket* ws)
{
if (ws == _client)
{
_client = NULL;
}
CC_SAFE_DELETE(ws);
_evt->dispatchCustomEvent(NodejsClient::CLOSE, nullptr);
}
void NodejsClient::onError(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::ErrorCode& error)
{
if (ws == _client)
{
int code = (int)error;
_evt->dispatchCustomEvent(NodejsClient::ERR, &code);
}
}