Closes websocket connection by dispatching a 'resetDirector' event.

This commit is contained in:
James Chen 2016-02-29 18:55:51 +08:00
parent 9af2f4e73a
commit 037051d9c4
4 changed files with 22 additions and 3 deletions

View File

@ -92,6 +92,7 @@ const char *Director::EVENT_AFTER_DRAW = "director_after_draw";
const char *Director::EVENT_AFTER_VISIT = "director_after_visit";
const char *Director::EVENT_BEFORE_UPDATE = "director_before_update";
const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
const char *Director::EVENT_RESET = "director_reset";
Director* Director::getInstance()
{
@ -165,6 +166,7 @@ bool Director::init(void)
_eventAfterUpdate->setUserData(this);
_eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
_eventProjectionChanged->setUserData(this);
_eventResetDirector = new (std::nothrow) EventCustom(EVENT_RESET);
//init TextureCache
initTextureCache();
initMatrixStack();
@ -194,6 +196,7 @@ Director::~Director(void)
delete _eventAfterDraw;
delete _eventAfterVisit;
delete _eventProjectionChanged;
delete _eventResetDirector;
delete _renderer;
@ -977,8 +980,7 @@ void Director::reset()
_runningScene = nullptr;
_nextScene = nullptr;
// Close all websocket connection. It has to be invoked before cleaning scheduler
network::WebSocket::closeAllConnections();
_eventDispatcher->dispatchEvent(_eventResetDirector);
// cleanup scheduler
getScheduler()->unscheduleAll();

View File

@ -102,6 +102,8 @@ public:
static const char* EVENT_BEFORE_UPDATE;
/** Director will trigger an event after Schedule::update() is invoked. */
static const char* EVENT_AFTER_UPDATE;
/** Director will trigger an event while resetting Director */
static const char* EVENT_RESET;
/** Director will trigger an event after Scene::render() is invoked. */
static const char* EVENT_AFTER_VISIT;
/** Director will trigger an event after a scene is drawn, the data is sent to GPU. */
@ -540,7 +542,7 @@ protected:
@since v3.0
*/
EventDispatcher* _eventDispatcher;
EventCustom *_eventProjectionChanged, *_eventAfterDraw, *_eventAfterVisit, *_eventBeforeUpdate, *_eventAfterUpdate;
EventCustom *_eventProjectionChanged, *_eventAfterDraw, *_eventAfterVisit, *_eventBeforeUpdate, *_eventAfterUpdate, *_eventResetDirector;
/* delta time since last tick to main loop */
float _deltaTime;

View File

@ -30,6 +30,8 @@
#include "WebSocket.h"
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include "base/CCEventDispatcher.h"
#include "base/CCEventListenerCustom.h"
#include <thread>
#include <mutex>
@ -324,6 +326,13 @@ WebSocket::WebSocket()
}
__websocketInstances->push_back(this);
std::shared_ptr<bool> isDestroyed = _isDestroyed;
_resetDirectorListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_RESET, [this, isDestroyed](EventCustom*){
if (*isDestroyed)
return;
close();
});
}
WebSocket::~WebSocket()
@ -352,6 +361,9 @@ WebSocket::~WebSocket()
LOGD("ERROR: WebSocket instance (%p) wasn't added to the container which saves websocket instances!\n", this);
}
}
Director::getInstance()->getEventDispatcher()->removeEventListener(_resetDirectorListener);
*_isDestroyed = true;
}

View File

@ -49,6 +49,8 @@ struct lws_protocols;
NS_CC_BEGIN
class EventListenerCustom;
namespace network {
class WsThreadHelper;
@ -240,6 +242,7 @@ private:
Delegate* _delegate;
int _SSLConnection;
struct lws_protocols* _wsProtocols;
EventListenerCustom* _resetDirectorListener;
};
}