diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 1ffd0325fc..7c97297db3 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -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(); diff --git a/cocos/base/CCDirector.h b/cocos/base/CCDirector.h index 1b51603054..aa0ab1b246 100644 --- a/cocos/base/CCDirector.h +++ b/cocos/base/CCDirector.h @@ -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; diff --git a/cocos/network/WebSocket.cpp b/cocos/network/WebSocket.cpp index 8ea5f727ec..d79cd9266f 100644 --- a/cocos/network/WebSocket.cpp +++ b/cocos/network/WebSocket.cpp @@ -30,6 +30,8 @@ #include "WebSocket.h" #include "base/CCDirector.h" #include "base/CCScheduler.h" +#include "base/CCEventDispatcher.h" +#include "base/CCEventListenerCustom.h" #include #include @@ -324,6 +326,13 @@ WebSocket::WebSocket() } __websocketInstances->push_back(this); + + std::shared_ptr 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; } diff --git a/cocos/network/WebSocket.h b/cocos/network/WebSocket.h index d98a2dbb68..f10f9b8187 100644 --- a/cocos/network/WebSocket.h +++ b/cocos/network/WebSocket.h @@ -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; }; }