mirror of https://github.com/axmolengine/axmol.git
Closes websocket connection by dispatching a 'resetDirector' event.
This commit is contained in:
parent
9af2f4e73a
commit
037051d9c4
|
@ -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_AFTER_VISIT = "director_after_visit";
|
||||||
const char *Director::EVENT_BEFORE_UPDATE = "director_before_update";
|
const char *Director::EVENT_BEFORE_UPDATE = "director_before_update";
|
||||||
const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
|
const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
|
||||||
|
const char *Director::EVENT_RESET = "director_reset";
|
||||||
|
|
||||||
Director* Director::getInstance()
|
Director* Director::getInstance()
|
||||||
{
|
{
|
||||||
|
@ -165,6 +166,7 @@ bool Director::init(void)
|
||||||
_eventAfterUpdate->setUserData(this);
|
_eventAfterUpdate->setUserData(this);
|
||||||
_eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
|
_eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
|
||||||
_eventProjectionChanged->setUserData(this);
|
_eventProjectionChanged->setUserData(this);
|
||||||
|
_eventResetDirector = new (std::nothrow) EventCustom(EVENT_RESET);
|
||||||
//init TextureCache
|
//init TextureCache
|
||||||
initTextureCache();
|
initTextureCache();
|
||||||
initMatrixStack();
|
initMatrixStack();
|
||||||
|
@ -194,6 +196,7 @@ Director::~Director(void)
|
||||||
delete _eventAfterDraw;
|
delete _eventAfterDraw;
|
||||||
delete _eventAfterVisit;
|
delete _eventAfterVisit;
|
||||||
delete _eventProjectionChanged;
|
delete _eventProjectionChanged;
|
||||||
|
delete _eventResetDirector;
|
||||||
|
|
||||||
delete _renderer;
|
delete _renderer;
|
||||||
|
|
||||||
|
@ -977,8 +980,7 @@ void Director::reset()
|
||||||
_runningScene = nullptr;
|
_runningScene = nullptr;
|
||||||
_nextScene = nullptr;
|
_nextScene = nullptr;
|
||||||
|
|
||||||
// Close all websocket connection. It has to be invoked before cleaning scheduler
|
_eventDispatcher->dispatchEvent(_eventResetDirector);
|
||||||
network::WebSocket::closeAllConnections();
|
|
||||||
|
|
||||||
// cleanup scheduler
|
// cleanup scheduler
|
||||||
getScheduler()->unscheduleAll();
|
getScheduler()->unscheduleAll();
|
||||||
|
|
|
@ -102,6 +102,8 @@ public:
|
||||||
static const char* EVENT_BEFORE_UPDATE;
|
static const char* EVENT_BEFORE_UPDATE;
|
||||||
/** Director will trigger an event after Schedule::update() is invoked. */
|
/** Director will trigger an event after Schedule::update() is invoked. */
|
||||||
static const char* EVENT_AFTER_UPDATE;
|
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. */
|
/** Director will trigger an event after Scene::render() is invoked. */
|
||||||
static const char* EVENT_AFTER_VISIT;
|
static const char* EVENT_AFTER_VISIT;
|
||||||
/** Director will trigger an event after a scene is drawn, the data is sent to GPU. */
|
/** Director will trigger an event after a scene is drawn, the data is sent to GPU. */
|
||||||
|
@ -540,7 +542,7 @@ protected:
|
||||||
@since v3.0
|
@since v3.0
|
||||||
*/
|
*/
|
||||||
EventDispatcher* _eventDispatcher;
|
EventDispatcher* _eventDispatcher;
|
||||||
EventCustom *_eventProjectionChanged, *_eventAfterDraw, *_eventAfterVisit, *_eventBeforeUpdate, *_eventAfterUpdate;
|
EventCustom *_eventProjectionChanged, *_eventAfterDraw, *_eventAfterVisit, *_eventBeforeUpdate, *_eventAfterUpdate, *_eventResetDirector;
|
||||||
|
|
||||||
/* delta time since last tick to main loop */
|
/* delta time since last tick to main loop */
|
||||||
float _deltaTime;
|
float _deltaTime;
|
||||||
|
|
|
@ -30,6 +30,8 @@
|
||||||
#include "WebSocket.h"
|
#include "WebSocket.h"
|
||||||
#include "base/CCDirector.h"
|
#include "base/CCDirector.h"
|
||||||
#include "base/CCScheduler.h"
|
#include "base/CCScheduler.h"
|
||||||
|
#include "base/CCEventDispatcher.h"
|
||||||
|
#include "base/CCEventListenerCustom.h"
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -324,6 +326,13 @@ WebSocket::WebSocket()
|
||||||
}
|
}
|
||||||
|
|
||||||
__websocketInstances->push_back(this);
|
__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()
|
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);
|
LOGD("ERROR: WebSocket instance (%p) wasn't added to the container which saves websocket instances!\n", this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_resetDirectorListener);
|
||||||
|
|
||||||
*_isDestroyed = true;
|
*_isDestroyed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,8 @@ struct lws_protocols;
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
class EventListenerCustom;
|
||||||
|
|
||||||
namespace network {
|
namespace network {
|
||||||
|
|
||||||
class WsThreadHelper;
|
class WsThreadHelper;
|
||||||
|
@ -240,6 +242,7 @@ private:
|
||||||
Delegate* _delegate;
|
Delegate* _delegate;
|
||||||
int _SSLConnection;
|
int _SSLConnection;
|
||||||
struct lws_protocols* _wsProtocols;
|
struct lws_protocols* _wsProtocols;
|
||||||
|
EventListenerCustom* _resetDirectorListener;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue