mirror of https://github.com/axmolengine/axmol.git
issue 2430:use enum class
This commit is contained in:
parent
f94a02ac01
commit
0b8de88ddc
|
@ -134,7 +134,7 @@ bool AssetsManager::checkUpdate()
|
||||||
|
|
||||||
if (res != 0)
|
if (res != 0)
|
||||||
{
|
{
|
||||||
sendErrorMessage(ERROR_NETWORK);
|
sendErrorMessage(ErrorCode::NETWORK);
|
||||||
CCLOG("can not get version file content, error code is %d", res);
|
CCLOG("can not get version file content, error code is %d", res);
|
||||||
curl_easy_cleanup(_curl);
|
curl_easy_cleanup(_curl);
|
||||||
return false;
|
return false;
|
||||||
|
@ -143,7 +143,7 @@ bool AssetsManager::checkUpdate()
|
||||||
string recordedVersion = UserDefault::getInstance()->getStringForKey(KEY_OF_VERSION);
|
string recordedVersion = UserDefault::getInstance()->getStringForKey(KEY_OF_VERSION);
|
||||||
if (recordedVersion == _version)
|
if (recordedVersion == _version)
|
||||||
{
|
{
|
||||||
sendErrorMessage(ERROR_NO_NEW_VERSION);
|
sendErrorMessage(ErrorCode::NO_NEW_VERSION);
|
||||||
CCLOG("there is not new version");
|
CCLOG("there is not new version");
|
||||||
// Set resource search path.
|
// Set resource search path.
|
||||||
setSearchPath();
|
setSearchPath();
|
||||||
|
@ -173,7 +173,7 @@ void AssetsManager::downloadAndUncompress()
|
||||||
// Uncompress zip file.
|
// Uncompress zip file.
|
||||||
if (! uncompress())
|
if (! uncompress())
|
||||||
{
|
{
|
||||||
sendErrorMessage(AssetsManager::ERROR_UNCOMPRESS);
|
sendErrorMessage(ErrorCode::UNCOMPRESS);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,7 +403,7 @@ bool AssetsManager::downLoad()
|
||||||
FILE *fp = fopen(outFileName.c_str(), "wb");
|
FILE *fp = fopen(outFileName.c_str(), "wb");
|
||||||
if (! fp)
|
if (! fp)
|
||||||
{
|
{
|
||||||
sendErrorMessage(ERROR_CREATE_FILE);
|
sendErrorMessage(ErrorCode::CREATE_FILE);
|
||||||
CCLOG("can not create file %s", outFileName.c_str());
|
CCLOG("can not create file %s", outFileName.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -420,7 +420,7 @@ bool AssetsManager::downLoad()
|
||||||
curl_easy_cleanup(_curl);
|
curl_easy_cleanup(_curl);
|
||||||
if (res != 0)
|
if (res != 0)
|
||||||
{
|
{
|
||||||
sendErrorMessage(ERROR_NETWORK);
|
sendErrorMessage(ErrorCode::NETWORK);
|
||||||
CCLOG("error when download package");
|
CCLOG("error when download package");
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -44,19 +44,19 @@ class AssetsManagerDelegateProtocol;
|
||||||
class AssetsManager
|
class AssetsManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum ErrorCode
|
enum class ErrorCode
|
||||||
{
|
{
|
||||||
// Error caused by creating a file to store downloaded data
|
// Error caused by creating a file to store downloaded data
|
||||||
ERROR_CREATE_FILE,
|
CREATE_FILE,
|
||||||
/** Error caused by network
|
/** Error caused by network
|
||||||
-- network unavaivable
|
-- network unavaivable
|
||||||
-- timeout
|
-- timeout
|
||||||
-- ...
|
-- ...
|
||||||
*/
|
*/
|
||||||
ERROR_NETWORK,
|
NETWORK,
|
||||||
/** There is not a new version
|
/** There is not a new version
|
||||||
*/
|
*/
|
||||||
ERROR_NO_NEW_VERSION,
|
NO_NEW_VERSION,
|
||||||
/** Error caused in uncompressing stage
|
/** Error caused in uncompressing stage
|
||||||
-- can not open zip file
|
-- can not open zip file
|
||||||
-- can not read file global information
|
-- can not read file global information
|
||||||
|
@ -64,13 +64,7 @@ public:
|
||||||
-- can not create a directory
|
-- can not create a directory
|
||||||
-- ...
|
-- ...
|
||||||
*/
|
*/
|
||||||
ERROR_UNCOMPRESS,
|
UNCOMPRESS,
|
||||||
|
|
||||||
// keep compatability
|
|
||||||
kCreateFile = ERROR_CREATE_FILE,
|
|
||||||
kNetwork = ERROR_NETWORK,
|
|
||||||
kNoNewVersion = ERROR_NO_NEW_VERSION,
|
|
||||||
kUncompress = ERROR_UNCOMPRESS,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* @brief Creates a AssetsManager with new package url, version code url and storage path.
|
/* @brief Creates a AssetsManager with new package url, version code url and storage path.
|
||||||
|
|
|
@ -100,20 +100,19 @@ SIOClientImpl::SIOClientImpl(const std::string& host, int port) :
|
||||||
s << host << ":" << port;
|
s << host << ":" << port;
|
||||||
_uri = s.str();
|
_uri = s.str();
|
||||||
|
|
||||||
_ws = NULL;
|
_ws = nullptr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SIOClientImpl::~SIOClientImpl() {
|
SIOClientImpl::~SIOClientImpl()
|
||||||
|
{
|
||||||
if(_connected) disconnect();
|
if (_connected) disconnect();
|
||||||
|
|
||||||
CC_SAFE_DELETE(_clients);
|
CC_SAFE_DELETE(_clients);
|
||||||
CC_SAFE_DELETE(_ws);
|
CC_SAFE_DELETE(_ws);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::handshake() {
|
void SIOClientImpl::handshake()
|
||||||
|
{
|
||||||
log("SIOClientImpl::handshake() called");
|
log("SIOClientImpl::handshake() called");
|
||||||
|
|
||||||
std::stringstream pre;
|
std::stringstream pre;
|
||||||
|
@ -135,8 +134,8 @@ void SIOClientImpl::handshake() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response) {
|
void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response)
|
||||||
|
{
|
||||||
log("SIOClientImpl::handshakeResponse() called");
|
log("SIOClientImpl::handshakeResponse() called");
|
||||||
|
|
||||||
if (0 != strlen(response->getHttpRequest()->getTag()))
|
if (0 != strlen(response->getHttpRequest()->getTag()))
|
||||||
|
@ -185,18 +184,21 @@ void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response
|
||||||
int heartbeat = 0, timeout = 0;
|
int heartbeat = 0, timeout = 0;
|
||||||
|
|
||||||
pos = res.find(":");
|
pos = res.find(":");
|
||||||
if(pos >= 0) {
|
if(pos >= 0)
|
||||||
|
{
|
||||||
sid = res.substr(0, pos);
|
sid = res.substr(0, pos);
|
||||||
res.erase(0, pos+1);
|
res.erase(0, pos+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = res.find(":");
|
pos = res.find(":");
|
||||||
if(pos >= 0){
|
if(pos >= 0)
|
||||||
|
{
|
||||||
heartbeat = atoi(res.substr(pos+1, res.size()).c_str());
|
heartbeat = atoi(res.substr(pos+1, res.size()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = res.find(":");
|
pos = res.find(":");
|
||||||
if(pos >= 0){
|
if(pos >= 0)
|
||||||
|
{
|
||||||
timeout = atoi(res.substr(pos+1, res.size()).c_str());
|
timeout = atoi(res.substr(pos+1, res.size()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,15 +212,15 @@ void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::openSocket() {
|
void SIOClientImpl::openSocket()
|
||||||
|
{
|
||||||
log("SIOClientImpl::openSocket() called");
|
log("SIOClientImpl::openSocket() called");
|
||||||
|
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
s << _uri << "/socket.io/1/websocket/" << _sid;
|
s << _uri << "/socket.io/1/websocket/" << _sid;
|
||||||
|
|
||||||
_ws = new WebSocket();
|
_ws = new WebSocket();
|
||||||
if(!_ws->init(*this, s.str()))
|
if (!_ws->init(*this, s.str()))
|
||||||
{
|
{
|
||||||
CC_SAFE_DELETE(_ws);
|
CC_SAFE_DELETE(_ws);
|
||||||
}
|
}
|
||||||
|
@ -226,23 +228,21 @@ void SIOClientImpl::openSocket() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SIOClientImpl::init() {
|
bool SIOClientImpl::init()
|
||||||
|
{
|
||||||
log("SIOClientImpl::init() successful");
|
log("SIOClientImpl::init() successful");
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::connect() {
|
void SIOClientImpl::connect()
|
||||||
|
{
|
||||||
this->handshake();
|
this->handshake();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::disconnect() {
|
void SIOClientImpl::disconnect()
|
||||||
|
{
|
||||||
if(_ws->getReadyState() == WebSocket::kStateOpen) {
|
if(_ws->getReadyState() == WebSocket::State::OPEN)
|
||||||
|
{
|
||||||
std::string s = "0::";
|
std::string s = "0::";
|
||||||
|
|
||||||
_ws->send(s);
|
_ws->send(s);
|
||||||
|
@ -250,7 +250,6 @@ void SIOClientImpl::disconnect() {
|
||||||
log("Disconnect sent");
|
log("Disconnect sent");
|
||||||
|
|
||||||
_ws->close();
|
_ws->close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
|
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
|
||||||
|
@ -258,79 +257,71 @@ void SIOClientImpl::disconnect() {
|
||||||
_connected = false;
|
_connected = false;
|
||||||
|
|
||||||
SocketIO::instance()->removeSocket(_uri);
|
SocketIO::instance()->removeSocket(_uri);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SIOClientImpl* SIOClientImpl::create(const std::string& host, int port) {
|
SIOClientImpl* SIOClientImpl::create(const std::string& host, int port)
|
||||||
|
{
|
||||||
SIOClientImpl *s = new SIOClientImpl(host, port);
|
SIOClientImpl *s = new SIOClientImpl(host, port);
|
||||||
|
|
||||||
if(s && s->init()) {
|
if (s && s->init())
|
||||||
|
{
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return nullptr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SIOClient* SIOClientImpl::getClient(const std::string& endpoint) {
|
SIOClient* SIOClientImpl::getClient(const std::string& endpoint)
|
||||||
|
{
|
||||||
return static_cast<SIOClient*>(_clients->objectForKey(endpoint));
|
return static_cast<SIOClient*>(_clients->objectForKey(endpoint));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client) {
|
void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
|
||||||
|
{
|
||||||
_clients->setObject(client, endpoint);
|
_clients->setObject(client, endpoint);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::connectToEndpoint(const std::string& endpoint) {
|
void SIOClientImpl::connectToEndpoint(const std::string& endpoint)
|
||||||
|
{
|
||||||
std::string path = endpoint == "/" ? "" : endpoint;
|
std::string path = endpoint == "/" ? "" : endpoint;
|
||||||
|
|
||||||
std::string s = "1::" + path;
|
std::string s = "1::" + path;
|
||||||
|
|
||||||
_ws->send(s);
|
_ws->send(s);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint) {
|
void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
|
||||||
|
{
|
||||||
_clients->removeObjectForKey(endpoint);
|
_clients->removeObjectForKey(endpoint);
|
||||||
|
|
||||||
if(_clients->count() == 0 || endpoint == "/") {
|
if(_clients->count() == 0 || endpoint == "/")
|
||||||
|
{
|
||||||
log("SIOClientImpl::disconnectFromEndpoint out of endpoints, checking for disconnect");
|
log("SIOClientImpl::disconnectFromEndpoint out of endpoints, checking for disconnect");
|
||||||
|
|
||||||
if(_connected) this->disconnect();
|
if(_connected) this->disconnect();
|
||||||
|
}
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
std::string path = endpoint == "/" ? "" : endpoint;
|
std::string path = endpoint == "/" ? "" : endpoint;
|
||||||
|
|
||||||
std::string s = "0::" + path;
|
std::string s = "0::" + path;
|
||||||
|
|
||||||
_ws->send(s);
|
_ws->send(s);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::heartbeat(float dt) {
|
void SIOClientImpl::heartbeat(float dt)
|
||||||
|
{
|
||||||
std::string s = "2::";
|
std::string s = "2::";
|
||||||
|
|
||||||
_ws->send(s);
|
_ws->send(s);
|
||||||
|
|
||||||
log("Heartbeat sent");
|
log("Heartbeat sent");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SIOClientImpl::send(std::string endpoint, std::string s) {
|
void SIOClientImpl::send(std::string endpoint, std::string s)
|
||||||
|
{
|
||||||
std::stringstream pre;
|
std::stringstream pre;
|
||||||
|
|
||||||
std::string path = endpoint == "/" ? "" : endpoint;
|
std::string path = endpoint == "/" ? "" : endpoint;
|
||||||
|
@ -342,11 +333,10 @@ void SIOClientImpl::send(std::string endpoint, std::string s) {
|
||||||
log("sending message: %s", msg.c_str());
|
log("sending message: %s", msg.c_str());
|
||||||
|
|
||||||
_ws->send(msg);
|
_ws->send(msg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::string args) {
|
void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::string args)
|
||||||
|
{
|
||||||
std::stringstream pre;
|
std::stringstream pre;
|
||||||
|
|
||||||
std::string path = endpoint == "/" ? "" : endpoint;
|
std::string path = endpoint == "/" ? "" : endpoint;
|
||||||
|
@ -358,33 +348,30 @@ void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::strin
|
||||||
log("emitting event with data: %s", msg.c_str());
|
log("emitting event with data: %s", msg.c_str());
|
||||||
|
|
||||||
_ws->send(msg);
|
_ws->send(msg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::onOpen(cocos2d::extension::WebSocket* ws) {
|
void SIOClientImpl::onOpen(cocos2d::extension::WebSocket* ws)
|
||||||
|
{
|
||||||
_connected = true;
|
_connected = true;
|
||||||
|
|
||||||
SocketIO::instance()->addSocket(_uri, this);
|
SocketIO::instance()->addSocket(_uri, this);
|
||||||
|
|
||||||
DictElement* e = NULL;
|
DictElement* e = NULL;
|
||||||
|
|
||||||
CCDICT_FOREACH(_clients, e) {
|
CCDICT_FOREACH(_clients, e)
|
||||||
|
{
|
||||||
SIOClient *c = static_cast<SIOClient*>(e->getObject());
|
SIOClient *c = static_cast<SIOClient*>(e->getObject());
|
||||||
|
|
||||||
c->onOpen();
|
c->onOpen();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Director::getInstance()->getScheduler()->scheduleSelector(schedule_selector(SIOClientImpl::heartbeat), this, (_heartbeat * .9), false);
|
Director::getInstance()->getScheduler()->scheduleSelector(schedule_selector(SIOClientImpl::heartbeat), this, (_heartbeat * .9), false);
|
||||||
|
|
||||||
log("SIOClientImpl::onOpen socket connected!");
|
log("SIOClientImpl::onOpen socket connected!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data) {
|
void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data)
|
||||||
|
{
|
||||||
log("SIOClientImpl::onMessage received: %s", data.bytes);
|
log("SIOClientImpl::onMessage received: %s", data.bytes);
|
||||||
|
|
||||||
int control = atoi(&data.bytes[0]);
|
int control = atoi(&data.bytes[0]);
|
||||||
|
@ -406,25 +393,26 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
||||||
payload.erase(0, pos+1);
|
payload.erase(0, pos+1);
|
||||||
|
|
||||||
pos = payload.find(":");
|
pos = payload.find(":");
|
||||||
if(pos >= 0) {
|
if(pos >= 0)
|
||||||
|
{
|
||||||
endpoint = payload.substr(0, pos);
|
endpoint = payload.substr(0, pos);
|
||||||
payload.erase(0, pos+1);
|
payload.erase(0, pos+1);
|
||||||
|
}
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
endpoint = payload;
|
endpoint = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(endpoint == "") endpoint = "/";
|
if (endpoint == "") endpoint = "/";
|
||||||
|
|
||||||
|
|
||||||
s_data = payload;
|
s_data = payload;
|
||||||
SIOClient *c = NULL;
|
SIOClient *c = NULL;
|
||||||
c = getClient(endpoint);
|
c = getClient(endpoint);
|
||||||
if(c == NULL) log("SIOClientImpl::onMessage client lookup returned NULL");
|
if (c == NULL) log("SIOClientImpl::onMessage client lookup returned NULL");
|
||||||
|
|
||||||
switch(control) {
|
switch(control)
|
||||||
|
{
|
||||||
case 0:
|
case 0:
|
||||||
log("Received Disconnect Signal for Endpoint: %s\n", endpoint.c_str());
|
log("Received Disconnect Signal for Endpoint: %s\n", endpoint.c_str());
|
||||||
if(c) c->receivedDisconnect();
|
if(c) c->receivedDisconnect();
|
||||||
|
@ -448,11 +436,13 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
||||||
case 5:
|
case 5:
|
||||||
log("Event Received with data: %s \n", s_data.c_str());
|
log("Event Received with data: %s \n", s_data.c_str());
|
||||||
|
|
||||||
if(c) {
|
if(c)
|
||||||
|
{
|
||||||
eventname = "";
|
eventname = "";
|
||||||
pos = s_data.find(":");
|
pos = s_data.find(":");
|
||||||
pos2 = s_data.find(",");
|
pos2 = s_data.find(",");
|
||||||
if(pos2 > pos) {
|
if(pos2 > pos)
|
||||||
|
{
|
||||||
s_data = s_data.substr(pos+1, pos2-pos-1);
|
s_data = s_data.substr(pos+1, pos2-pos-1);
|
||||||
std::remove_copy(s_data.begin(), s_data.end(),
|
std::remove_copy(s_data.begin(), s_data.end(),
|
||||||
std::back_inserter(eventname), '"');
|
std::back_inserter(eventname), '"');
|
||||||
|
@ -477,29 +467,25 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::onClose(cocos2d::extension::WebSocket* ws) {
|
void SIOClientImpl::onClose(cocos2d::extension::WebSocket* ws)
|
||||||
|
{
|
||||||
if(_clients->count() > 0) {
|
if(_clients->count() > 0)
|
||||||
|
{
|
||||||
DictElement *e;
|
DictElement *e;
|
||||||
|
|
||||||
CCDICT_FOREACH(_clients, e) {
|
CCDICT_FOREACH(_clients, e)
|
||||||
|
{
|
||||||
SIOClient *c = static_cast<SIOClient *>(e->getObject());
|
SIOClient *c = static_cast<SIOClient *>(e->getObject());
|
||||||
|
|
||||||
c->receivedDisconnect();
|
c->receivedDisconnect();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this->release();
|
this->release();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClientImpl::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error) {
|
void SIOClientImpl::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//begin SIOClient methods
|
//begin SIOClient methods
|
||||||
|
@ -512,56 +498,58 @@ SIOClient::SIOClient(const std::string& host, int port, const std::string& path,
|
||||||
, _delegate(&delegate)
|
, _delegate(&delegate)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SIOClient::~SIOClient(void) {
|
SIOClient::~SIOClient(void)
|
||||||
|
{
|
||||||
if(_connected) {
|
if (_connected)
|
||||||
|
{
|
||||||
_socket->disconnectFromEndpoint(_path);
|
_socket->disconnectFromEndpoint(_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::onOpen() {
|
void SIOClient::onOpen()
|
||||||
|
{
|
||||||
if(_path != "/") {
|
if (_path != "/")
|
||||||
|
{
|
||||||
_socket->connectToEndpoint(_path);
|
_socket->connectToEndpoint(_path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::onConnect() {
|
void SIOClient::onConnect()
|
||||||
|
{
|
||||||
_connected = true;
|
_connected = true;
|
||||||
_delegate->onConnect(this);
|
_delegate->onConnect(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::send(std::string s) {
|
void SIOClient::send(std::string s)
|
||||||
|
{
|
||||||
if(_connected) {
|
if (_connected)
|
||||||
|
{
|
||||||
_socket->send(_path, s);
|
_socket->send(_path, s);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_delegate->onError(this, "Client not yet connected");
|
_delegate->onError(this, "Client not yet connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::emit(std::string eventname, std::string args) {
|
void SIOClient::emit(std::string eventname, std::string args)
|
||||||
|
{
|
||||||
if(_connected) {
|
if(_connected)
|
||||||
|
{
|
||||||
_socket->emit(_path, eventname, args);
|
_socket->emit(_path, eventname, args);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_delegate->onError(this, "Client not yet connected");
|
_delegate->onError(this, "Client not yet connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::disconnect() {
|
void SIOClient::disconnect()
|
||||||
|
{
|
||||||
_connected = false;
|
_connected = false;
|
||||||
|
|
||||||
_socket->disconnectFromEndpoint(_path);
|
_socket->disconnectFromEndpoint(_path);
|
||||||
|
@ -569,31 +557,28 @@ void SIOClient::disconnect() {
|
||||||
_delegate->onClose(this);
|
_delegate->onClose(this);
|
||||||
|
|
||||||
this->release();
|
this->release();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::receivedDisconnect() {
|
void SIOClient::receivedDisconnect()
|
||||||
|
{
|
||||||
_connected = false;
|
_connected = false;
|
||||||
|
|
||||||
_delegate->onClose(this);
|
_delegate->onClose(this);
|
||||||
|
|
||||||
this->release();
|
this->release();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::on(const std::string& eventName, SIOEvent e) {
|
void SIOClient::on(const std::string& eventName, SIOEvent e)
|
||||||
|
{
|
||||||
_eventRegistry[eventName] = e;
|
_eventRegistry[eventName] = e;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SIOClient::fireEvent(const std::string& eventName, const std::string& data) {
|
void SIOClient::fireEvent(const std::string& eventName, const std::string& data)
|
||||||
|
{
|
||||||
log("SIOClient::fireEvent called with event name: %s and data: %s", eventName.c_str(), data.c_str());
|
log("SIOClient::fireEvent called with event name: %s and data: %s", eventName.c_str(), data.c_str());
|
||||||
|
|
||||||
if(_eventRegistry[eventName]) {
|
if(_eventRegistry[eventName])
|
||||||
|
{
|
||||||
SIOEvent e = _eventRegistry[eventName];
|
SIOEvent e = _eventRegistry[eventName];
|
||||||
|
|
||||||
e(this, data);
|
e(this, data);
|
||||||
|
@ -602,71 +587,75 @@ void SIOClient::fireEvent(const std::string& eventName, const std::string& data)
|
||||||
}
|
}
|
||||||
|
|
||||||
log("SIOClient::fireEvent no event with name %s found", eventName.c_str());
|
log("SIOClient::fireEvent no event with name %s found", eventName.c_str());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//begin SocketIO methods
|
//begin SocketIO methods
|
||||||
SocketIO *SocketIO::_inst = NULL;
|
SocketIO *SocketIO::_inst = nullptr;
|
||||||
|
|
||||||
SocketIO::SocketIO() {
|
|
||||||
|
|
||||||
|
SocketIO::SocketIO()
|
||||||
|
{
|
||||||
_sockets = Dictionary::create();
|
_sockets = Dictionary::create();
|
||||||
_sockets->retain();
|
_sockets->retain();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketIO::~SocketIO(void) {
|
SocketIO::~SocketIO(void)
|
||||||
|
{
|
||||||
CC_SAFE_DELETE(_sockets);
|
CC_SAFE_DELETE(_sockets);
|
||||||
delete _inst;
|
delete _inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketIO* SocketIO::instance() {
|
SocketIO* SocketIO::instance()
|
||||||
|
{
|
||||||
if(!_inst)
|
if(!_inst) _inst = new SocketIO();
|
||||||
_inst = new SocketIO();
|
|
||||||
|
|
||||||
return _inst;
|
return _inst;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string& uri) {
|
SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string& uri)
|
||||||
|
{
|
||||||
std::string host = uri;
|
std::string host = uri;
|
||||||
int port = 0;
|
int port = 0;
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
pos = host.find("//");
|
pos = host.find("//");
|
||||||
if(pos >= 0) {
|
if (pos >= 0)
|
||||||
|
{
|
||||||
host.erase(0, pos+2);
|
host.erase(0, pos+2);
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = host.find(":");
|
pos = host.find(":");
|
||||||
if(pos >= 0){
|
if (pos >= 0)
|
||||||
|
{
|
||||||
port = atoi(host.substr(pos+1, host.size()).c_str());
|
port = atoi(host.substr(pos+1, host.size()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = host.find("/", 0);
|
pos = host.find("/", 0);
|
||||||
std::string path = "/";
|
std::string path = "/";
|
||||||
if(pos >= 0){
|
if (pos >= 0)
|
||||||
|
{
|
||||||
path += host.substr(pos + 1, host.size());
|
path += host.substr(pos + 1, host.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = host.find(":");
|
pos = host.find(":");
|
||||||
if(pos >= 0){
|
if (pos >= 0)
|
||||||
|
{
|
||||||
host.erase(pos, host.size());
|
host.erase(pos, host.size());
|
||||||
}else if((pos = host.find("/"))>=0) {
|
}
|
||||||
|
else if ((pos = host.find("/"))>=0)
|
||||||
|
{
|
||||||
host.erase(pos, host.size());
|
host.erase(pos, host.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
s << host << ":" << port;
|
s << host << ":" << port;
|
||||||
|
|
||||||
SIOClientImpl* socket = NULL;
|
SIOClientImpl* socket = nullptr;
|
||||||
SIOClient *c = NULL;
|
SIOClient *c = nullptr;
|
||||||
|
|
||||||
socket = SocketIO::instance()->getSocket(s.str());
|
socket = SocketIO::instance()->getSocket(s.str());
|
||||||
|
|
||||||
if(socket == NULL) {
|
if(socket == nullptr)
|
||||||
|
{
|
||||||
//create a new socket, new client, connect
|
//create a new socket, new client, connect
|
||||||
socket = SIOClientImpl::create(host, port);
|
socket = SIOClientImpl::create(host, port);
|
||||||
|
|
||||||
|
@ -675,40 +664,37 @@ SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string&
|
||||||
socket->addClient(path, c);
|
socket->addClient(path, c);
|
||||||
|
|
||||||
socket->connect();
|
socket->connect();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
} else {
|
|
||||||
//check if already connected to endpoint, handle
|
//check if already connected to endpoint, handle
|
||||||
c = socket->getClient(path);
|
c = socket->getClient(path);
|
||||||
|
|
||||||
if(c == NULL) {
|
if(c == NULL)
|
||||||
|
{
|
||||||
c = new SIOClient(host, port, path, socket, delegate);
|
c = new SIOClient(host, port, path, socket, delegate);
|
||||||
|
|
||||||
socket->addClient(path, c);
|
socket->addClient(path, c);
|
||||||
|
|
||||||
socket->connectToEndpoint(path);
|
socket->connectToEndpoint(path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SIOClientImpl* SocketIO::getSocket(const std::string& uri) {
|
SIOClientImpl* SocketIO::getSocket(const std::string& uri)
|
||||||
|
{
|
||||||
return static_cast<SIOClientImpl*>(_sockets->objectForKey(uri));
|
return static_cast<SIOClientImpl*>(_sockets->objectForKey(uri));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket) {
|
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket)
|
||||||
|
{
|
||||||
_sockets->setObject(socket, uri);
|
_sockets->setObject(socket, uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketIO::removeSocket(const std::string& uri) {
|
void SocketIO::removeSocket(const std::string& uri)
|
||||||
|
{
|
||||||
_sockets->removeObjectForKey(uri);
|
_sockets->removeObjectForKey(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,14 +211,14 @@ enum WS_MSG {
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocket::WebSocket()
|
WebSocket::WebSocket()
|
||||||
: _readyState(kStateConnecting)
|
: _readyState(State::CONNECTING)
|
||||||
, _port(80)
|
, _port(80)
|
||||||
, _wsHelper(NULL)
|
, _wsHelper(nullptr)
|
||||||
, _wsInstance(NULL)
|
, _wsInstance(nullptr)
|
||||||
, _wsContext(NULL)
|
, _wsContext(nullptr)
|
||||||
, _delegate(NULL)
|
, _delegate(nullptr)
|
||||||
, _SSLConnection(0)
|
, _SSLConnection(0)
|
||||||
, _wsProtocols(NULL)
|
, _wsProtocols(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +227,8 @@ WebSocket::~WebSocket()
|
||||||
close();
|
close();
|
||||||
CC_SAFE_RELEASE_NULL(_wsHelper);
|
CC_SAFE_RELEASE_NULL(_wsHelper);
|
||||||
|
|
||||||
for (int i = 0; _wsProtocols[i].callback != NULL; ++i) {
|
for (int i = 0; _wsProtocols[i].callback != nullptr; ++i)
|
||||||
|
{
|
||||||
CC_SAFE_DELETE_ARRAY(_wsProtocols[i].name);
|
CC_SAFE_DELETE_ARRAY(_wsProtocols[i].name);
|
||||||
}
|
}
|
||||||
CC_SAFE_DELETE_ARRAY(_wsProtocols);
|
CC_SAFE_DELETE_ARRAY(_wsProtocols);
|
||||||
|
@ -247,9 +248,7 @@ bool WebSocket::init(const Delegate& delegate,
|
||||||
|
|
||||||
//ws://
|
//ws://
|
||||||
pos = host.find("ws://");
|
pos = host.find("ws://");
|
||||||
if (pos == 0){
|
if (pos == 0) host.erase(0,5);
|
||||||
host.erase(0,5);
|
|
||||||
}
|
|
||||||
|
|
||||||
pos = host.find("wss://");
|
pos = host.find("wss://");
|
||||||
if (pos == 0)
|
if (pos == 0)
|
||||||
|
@ -259,15 +258,11 @@ bool WebSocket::init(const Delegate& delegate,
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = host.find(":");
|
pos = host.find(":");
|
||||||
if(pos >= 0){
|
if (pos >= 0) port = atoi(host.substr(pos+1, host.size()).c_str());
|
||||||
port = atoi(host.substr(pos+1, host.size()).c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
pos = host.find("/", 0);
|
pos = host.find("/", 0);
|
||||||
std::string path = "/";
|
std::string path = "/";
|
||||||
if(pos >= 0){
|
if (pos >= 0) path += host.substr(pos + 1, host.size());
|
||||||
path += host.substr(pos + 1, host.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
pos = host.find(":");
|
pos = host.find(":");
|
||||||
if(pos >= 0){
|
if(pos >= 0){
|
||||||
|
@ -299,7 +294,8 @@ bool WebSocket::init(const Delegate& delegate,
|
||||||
if (protocols)
|
if (protocols)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (std::vector<std::string>::const_iterator iter = protocols->begin(); iter != protocols->end(); ++iter, ++i) {
|
for (std::vector<std::string>::const_iterator iter = protocols->begin(); iter != protocols->end(); ++iter, ++i)
|
||||||
|
{
|
||||||
char* name = new char[(*iter).length()+1];
|
char* name = new char[(*iter).length()+1];
|
||||||
strcpy(name, (*iter).c_str());
|
strcpy(name, (*iter).c_str());
|
||||||
_wsProtocols[i].name = name;
|
_wsProtocols[i].name = name;
|
||||||
|
@ -323,7 +319,7 @@ bool WebSocket::init(const Delegate& delegate,
|
||||||
|
|
||||||
void WebSocket::send(const std::string& message)
|
void WebSocket::send(const std::string& message)
|
||||||
{
|
{
|
||||||
if (_readyState == kStateOpen)
|
if (_readyState == State::OPEN)
|
||||||
{
|
{
|
||||||
// In main thread
|
// In main thread
|
||||||
WsMessage* msg = new WsMessage();
|
WsMessage* msg = new WsMessage();
|
||||||
|
@ -339,9 +335,9 @@ void WebSocket::send(const std::string& message)
|
||||||
|
|
||||||
void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
|
void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
|
||||||
{
|
{
|
||||||
CCASSERT(binaryMsg != NULL && len > 0, "parameter invalid.");
|
CCASSERT(binaryMsg != nullptr && len > 0, "parameter invalid.");
|
||||||
|
|
||||||
if (_readyState == kStateOpen)
|
if (_readyState == State::OPEN)
|
||||||
{
|
{
|
||||||
// In main thread
|
// In main thread
|
||||||
WsMessage* msg = new WsMessage();
|
WsMessage* msg = new WsMessage();
|
||||||
|
@ -359,13 +355,13 @@ void WebSocket::close()
|
||||||
{
|
{
|
||||||
Director::getInstance()->getScheduler()->unscheduleAllForTarget(_wsHelper);
|
Director::getInstance()->getScheduler()->unscheduleAllForTarget(_wsHelper);
|
||||||
|
|
||||||
if (_readyState == kStateClosing || _readyState == kStateClosed)
|
if (_readyState == State::CLOSING || _readyState == State::CLOSED)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CCLOG("websocket (%p) connection closed by client", this);
|
CCLOG("websocket (%p) connection closed by client", this);
|
||||||
_readyState = kStateClosed;
|
_readyState = State::CLOSED;
|
||||||
|
|
||||||
_wsHelper->joinSubThread();
|
_wsHelper->joinSubThread();
|
||||||
|
|
||||||
|
@ -381,14 +377,14 @@ WebSocket::State WebSocket::getReadyState()
|
||||||
|
|
||||||
int WebSocket::onSubThreadLoop()
|
int WebSocket::onSubThreadLoop()
|
||||||
{
|
{
|
||||||
if (_readyState == kStateClosed || _readyState == kStateClosing)
|
if (_readyState == State::CLOSED || _readyState == State::CLOSING)
|
||||||
{
|
{
|
||||||
libwebsocket_context_destroy(_wsContext);
|
libwebsocket_context_destroy(_wsContext);
|
||||||
// return 1 to exit the loop.
|
// return 1 to exit the loop.
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_wsContext && _readyState != kStateClosed && _readyState != kStateClosing)
|
if (_wsContext && _readyState != State::CLOSED && _readyState != State::CLOSING)
|
||||||
{
|
{
|
||||||
libwebsocket_service(_wsContext, 0);
|
libwebsocket_service(_wsContext, 0);
|
||||||
}
|
}
|
||||||
|
@ -424,15 +420,15 @@ void WebSocket::onSubThreadStarted()
|
||||||
|
|
||||||
_wsContext = libwebsocket_create_context(&info);
|
_wsContext = libwebsocket_create_context(&info);
|
||||||
|
|
||||||
if(NULL != _wsContext){
|
if(nullptr != _wsContext)
|
||||||
_readyState = kStateConnecting;
|
{
|
||||||
|
_readyState = State::CONNECTING;
|
||||||
std::string name;
|
std::string name;
|
||||||
for (int i = 0; _wsProtocols[i].callback != NULL; ++i) {
|
for (int i = 0; _wsProtocols[i].callback != nullptr; ++i)
|
||||||
|
{
|
||||||
name += (_wsProtocols[i].name);
|
name += (_wsProtocols[i].name);
|
||||||
if (_wsProtocols[i+1].callback != NULL)
|
|
||||||
{
|
if (_wsProtocols[i+1].callback != nullptr) name += ", ";
|
||||||
name += ", ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_wsInstance = libwebsocket_client_connect(_wsContext, _host.c_str(), _port, _SSLConnection,
|
_wsInstance = libwebsocket_client_connect(_wsContext, _host.c_str(), _port, _SSLConnection,
|
||||||
_path.c_str(), _host.c_str(), _host.c_str(),
|
_path.c_str(), _host.c_str(), _host.c_str(),
|
||||||
|
@ -451,8 +447,8 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
void *user, void *in, size_t len)
|
void *user, void *in, size_t len)
|
||||||
{
|
{
|
||||||
//CCLOG("socket callback for %d reason", reason);
|
//CCLOG("socket callback for %d reason", reason);
|
||||||
CCASSERT(_wsContext == NULL || ctx == _wsContext, "Invalid context.");
|
CCASSERT(_wsContext == nullptr || ctx == _wsContext, "Invalid context.");
|
||||||
CCASSERT(_wsInstance == NULL || wsi == NULL || wsi == _wsInstance, "Invaild websocket instance.");
|
CCASSERT(_wsInstance == nullptr || wsi == nullptr || wsi == _wsInstance, "Invaild websocket instance.");
|
||||||
|
|
||||||
switch (reason)
|
switch (reason)
|
||||||
{
|
{
|
||||||
|
@ -460,17 +456,17 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
||||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||||
{
|
{
|
||||||
WsMessage* msg = NULL;
|
WsMessage* msg = nullptr;
|
||||||
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|
||||||
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateConnecting)
|
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == State::CONNECTING)
|
||||||
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == kStateConnecting)
|
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == State::CONNECTING)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
msg = new WsMessage();
|
msg = new WsMessage();
|
||||||
msg->what = WS_MSG_TO_UITHREAD_ERROR;
|
msg->what = WS_MSG_TO_UITHREAD_ERROR;
|
||||||
_readyState = kStateClosing;
|
_readyState = State::CLOSING;
|
||||||
}
|
}
|
||||||
else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateClosing)
|
else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == State::CLOSING)
|
||||||
{
|
{
|
||||||
msg = new WsMessage();
|
msg = new WsMessage();
|
||||||
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
||||||
|
@ -486,7 +482,8 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
{
|
{
|
||||||
WsMessage* msg = new WsMessage();
|
WsMessage* msg = new WsMessage();
|
||||||
msg->what = WS_MSG_TO_UITHREAD_OPEN;
|
msg->what = WS_MSG_TO_UITHREAD_OPEN;
|
||||||
_readyState = kStateOpen;
|
_readyState = State::OPEN;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* start the ball rolling,
|
* start the ball rolling,
|
||||||
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
||||||
|
@ -503,8 +500,8 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue->begin();
|
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue->begin();
|
||||||
|
|
||||||
int bytesWrite = 0;
|
int bytesWrite = 0;
|
||||||
for (; iter != _wsHelper->_subThreadWsMessageQueue->end(); ++iter) {
|
for (; iter != _wsHelper->_subThreadWsMessageQueue->end(); ++iter)
|
||||||
|
{
|
||||||
WsMessage* subThreadMsg = *iter;
|
WsMessage* subThreadMsg = *iter;
|
||||||
|
|
||||||
if ( WS_MSG_TO_SUBTRHEAD_SENDING_STRING == subThreadMsg->what
|
if ( WS_MSG_TO_SUBTRHEAD_SENDING_STRING == subThreadMsg->what
|
||||||
|
@ -531,10 +528,12 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
|
|
||||||
bytesWrite = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], data->len, writeProtocol);
|
bytesWrite = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], data->len, writeProtocol);
|
||||||
|
|
||||||
if (bytesWrite < 0) {
|
if (bytesWrite < 0)
|
||||||
|
{
|
||||||
CCLOGERROR("%s", "libwebsocket_write error...");
|
CCLOGERROR("%s", "libwebsocket_write error...");
|
||||||
}
|
}
|
||||||
if (bytesWrite < data->len) {
|
if (bytesWrite < data->len)
|
||||||
|
{
|
||||||
CCLOGERROR("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
|
CCLOGERROR("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -562,10 +561,10 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
|
|
||||||
_wsHelper->quitSubThread();
|
_wsHelper->quitSubThread();
|
||||||
|
|
||||||
if (_readyState != kStateClosed)
|
if (_readyState != State::CLOSED)
|
||||||
{
|
{
|
||||||
WsMessage* msg = new WsMessage();
|
WsMessage* msg = new WsMessage();
|
||||||
_readyState = kStateClosed;
|
_readyState = State::CLOSED;
|
||||||
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
||||||
_wsHelper->sendMessageToUIThread(msg);
|
_wsHelper->sendMessageToUIThread(msg);
|
||||||
}
|
}
|
||||||
|
@ -637,7 +636,7 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
|
||||||
case WS_MSG_TO_UITHREAD_ERROR:
|
case WS_MSG_TO_UITHREAD_ERROR:
|
||||||
{
|
{
|
||||||
// FIXME: The exact error needs to be checked.
|
// FIXME: The exact error needs to be checked.
|
||||||
WebSocket::ErrorCode err = ERROR_CONNECTION_FAILURE;
|
WebSocket::ErrorCode err = ErrorCode::CONNECTION_FAILURE;
|
||||||
_delegate->onError(this, err);
|
_delegate->onError(this, err);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -63,15 +63,22 @@ public:
|
||||||
/**
|
/**
|
||||||
* @brief Errors in websocket
|
* @brief Errors in websocket
|
||||||
*/
|
*/
|
||||||
enum ErrorCode
|
enum class ErrorCode
|
||||||
{
|
{
|
||||||
ERROR_TIME_OUT,
|
TIME_OUT,
|
||||||
ERROR_CONNECTION_FAILURE,
|
CONNECTION_FAILURE,
|
||||||
ERROR_UNKNOWN,
|
UNKNOWN,
|
||||||
|
};
|
||||||
kErrorTimeout = ERROR_TIME_OUT,
|
|
||||||
kErrorConnectionFailure = ERROR_CONNECTION_FAILURE,
|
/**
|
||||||
kErrorUnknown = ERROR_UNKNOWN,
|
* Websocket state
|
||||||
|
*/
|
||||||
|
enum class State
|
||||||
|
{
|
||||||
|
CONNECTING,
|
||||||
|
OPEN,
|
||||||
|
CLOSING,
|
||||||
|
CLOSED,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,17 +120,6 @@ public:
|
||||||
* @brief Closes the connection to server.
|
* @brief Closes the connection to server.
|
||||||
*/
|
*/
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
/**
|
|
||||||
* Websocket state
|
|
||||||
*/
|
|
||||||
enum State
|
|
||||||
{
|
|
||||||
kStateConnecting = 0,
|
|
||||||
kStateOpen,
|
|
||||||
kStateClosing,
|
|
||||||
kStateClosed
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Gets current state of connection.
|
* @brief Gets current state of connection.
|
||||||
|
|
|
@ -201,7 +201,7 @@ void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::Object *sender)
|
||||||
// Menu Callbacks
|
// Menu Callbacks
|
||||||
void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::Object *sender)
|
void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::Object *sender)
|
||||||
{
|
{
|
||||||
if (_wsiSendText->getReadyState() == WebSocket::kStateOpen)
|
if (_wsiSendText->getReadyState() == WebSocket::State::OPEN)
|
||||||
{
|
{
|
||||||
_sendTextStatus->setString("Send Text WS is waiting...");
|
_sendTextStatus->setString("Send Text WS is waiting...");
|
||||||
_wsiSendText->send("Hello WebSocket, I'm a text message.");
|
_wsiSendText->send("Hello WebSocket, I'm a text message.");
|
||||||
|
@ -216,7 +216,7 @@ void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::Object *sender)
|
||||||
|
|
||||||
void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::Object *sender)
|
void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::Object *sender)
|
||||||
{
|
{
|
||||||
if (_wsiSendBinary->getReadyState() == WebSocket::kStateOpen)
|
if (_wsiSendBinary->getReadyState() == WebSocket::State::OPEN)
|
||||||
{
|
{
|
||||||
_sendBinaryStatus->setString("Send Binary WS is waiting...");
|
_sendBinaryStatus->setString("Send Binary WS is waiting...");
|
||||||
char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";
|
char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";
|
||||||
|
|
Loading…
Reference in New Issue