From d2dbc9a7eff684122617d0c4df53ebe35a2105f9 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 7 Dec 2017 17:10:18 +0800 Subject: [PATCH 1/2] Workaround for socketio.emit without payload argument (#1019) --- .../js-bindings/manual/jsb_socketio.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp b/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp index 39afd9cde2..8b64806bfe 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp @@ -180,7 +180,7 @@ static bool SocketIO_emit(se::State& s) int argc = (int)args.size(); SIOClient* cobj = (SIOClient*)s.nativeThisObject(); - if (argc == 2) + if (argc >= 1) { bool ok = false; std::string eventName; @@ -188,10 +188,19 @@ static bool SocketIO_emit(se::State& s) SE_PRECONDITION2(ok, false, "Converting eventName failed!"); std::string payload; - ok = seval_to_std_string(args[1], &payload); - SE_PRECONDITION2(ok, false, "Converting payload failed!"); - - CCLOG("JSB SocketIO emit event '%s' with payload: %s", eventName.c_str(), payload.c_str()); + if (argc >= 2) + { + const auto& arg1 = args[1]; + // Add this check to make it compatible with old version. + // jsval_to_std_string in v1.6 returns empty string if arg1 is null or undefined + // while seval_to_std_string since 1.7.2 follows JS standard to return "null" or "undefined". + // Therefore, we need a workaround to make it be compatible with versions lower than v1.7. + if (!arg1.isNullOrUndefined()) + { + ok = seval_to_std_string(arg1, &payload); + SE_PRECONDITION2(ok, false, "Converting payload failed!"); + } + } cobj->emit(eventName, payload); return true; From 0a0a177ee72a4eec2cb2fbd7a94da5dc5b97f9d4 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 7 Dec 2017 17:20:44 +0800 Subject: [PATCH 2/2] Fixes xhr.ontimeout isn't invoked and xhr.onerror is called instead while timeout is triggered. (#1020) --- .../js-bindings/manual/jsb_xmlhttprequest.cpp | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp b/native/cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp index 7612dd434f..a01cd64e09 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp @@ -106,6 +106,7 @@ private: cocos2d::network::HttpRequest* _httpRequest; cocos2d::EventListenerCustom* _resetDirectorListener; + unsigned long _timeoutInMilliseconds; uint16_t _status; ResponseType _responseType; @@ -128,6 +129,7 @@ XMLHttpRequest::XMLHttpRequest() , onerror(nullptr) , ontimeout(nullptr) , _httpRequest(new (std::nothrow) HttpRequest()) +, _timeoutInMilliseconds(0UL) , _status(0) , _responseType(ResponseType:: STRING) , _readyState(ReadyState::UNSENT) @@ -150,7 +152,9 @@ XMLHttpRequest::XMLHttpRequest() XMLHttpRequest::~XMLHttpRequest() { - cocos2d::Director::getInstance()->getEventDispatcher()->removeEventListener(_resetDirectorListener); + Director::getInstance()->getEventDispatcher()->removeEventListener(_resetDirectorListener); + Director::getInstance()->getScheduler()->unscheduleAllForTarget(this); + CC_SAFE_RELEASE(_httpRequest); } @@ -205,7 +209,8 @@ void XMLHttpRequest::sendBinary(const Data& data) void XMLHttpRequest::setTimeout(unsigned long timeoutInMilliseconds) { - _httpRequest->setTimeout(timeoutInMilliseconds / 1000.0f); + _timeoutInMilliseconds = timeoutInMilliseconds; + _httpRequest->setTimeout(timeoutInMilliseconds / 1000.0f + 2.0f); // Add 2 seconds more to ensure the timeout scheduler is invoked before http response. } unsigned long XMLHttpRequest::getTimeout() const @@ -322,6 +327,8 @@ void XMLHttpRequest::getHeader(const std::string& header) void XMLHttpRequest::onResponse(HttpClient* client, HttpResponse* response) { + Director::getInstance()->getScheduler()->unscheduleAllForTarget(this); + if (_isAborted || _readyState == ReadyState::UNSENT) { return; @@ -405,6 +412,20 @@ void XMLHttpRequest::onResponse(HttpClient* client, HttpResponse* response) void XMLHttpRequest::sendRequest() { + if (_timeoutInMilliseconds > 0) + { + Director::getInstance()->getScheduler()->schedule([this](float dt){ + if (ontimeout != nullptr) + ontimeout(); + + _readyState = ReadyState::UNSENT; + + _isLoadEnd = true; + if (onloadend != nullptr) + onloadend(); + + }, this, _timeoutInMilliseconds / 1000.0f, 0, 0.0f, false, "XMLHttpRequest"); + } setHttpRequestHeader(); _httpRequest->setResponseCallback(CC_CALLBACK_2(XMLHttpRequest::onResponse, this));