diff --git a/unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs b/unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs index 68c37c147c..ddee2a6203 100644 --- a/unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs +++ b/unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs @@ -130,7 +130,9 @@ class WebSocket extends EventTarget { close(code, data) { try { this._raw.close(code, data); - } catch(e) {} + } catch(e) { + this.dispatchEvent({type:'error', data: e.message}); //dispatchEvent immediately + } this._cleanup(); } diff --git a/unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp b/unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp index 9d6b9423fc..1f44038702 100644 --- a/unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp @@ -109,7 +109,7 @@ class V8WebSocketClientImpl void Close(const v8::FunctionCallbackInfo& Info); - void Close(websocketpp::close::status::value const code, std::string const& reason); + void CloseImmediately(websocketpp::close::status::value const code, std::string const& reason); void PollOne(); @@ -143,7 +143,7 @@ class V8WebSocketClientImpl static void OnGarbageCollectedWithFree(const v8::WeakCallbackInfo& Data) { // UE_LOG(LogTemp, Warning, TEXT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> auto gc %p"), Data.GetParameter()); - Data.GetParameter()->Close(websocketpp::close::status::normal, ""); + Data.GetParameter()->CloseImmediately(websocketpp::close::status::normal, ""); delete Data.GetParameter(); } @@ -158,7 +158,9 @@ V8WebSocketClientImpl::V8WebSocketClientImpl(v8::Isolate* InIsolate, v8::Local on_tls_init(websocketpp::connection_hdl) { auto ctx = websocketpp::lib::make_shared(websocketpp::lib::puerts_asio::ssl::context::sslv23); - ctx->set_verify_mode(puerts_asio::ssl::verify_none); + + websocketpp::lib::error_code ec; + ctx->set_verify_mode(puerts_asio::ssl::verify_none, ec); return ctx; } #endif @@ -293,14 +295,24 @@ void V8WebSocketClientImpl::Close(const v8::FunctionCallbackInfo& Inf reason = *v8::String::Utf8Value(InIsolate, Info[1]); } - Close(code, reason); + if (!Handle.expired()) + { + websocketpp::lib::error_code ec; + Client.close(Handle, code, reason, ec); + if (ec) + { + FV8Utils::ThrowException(Isolate, ec.message().c_str()); + } + } + Cleanup(); } -void V8WebSocketClientImpl::Close(websocketpp::close::status::value const code, std::string const& reason) +void V8WebSocketClientImpl::CloseImmediately(websocketpp::close::status::value const code, std::string const& reason) { if (!Handle.expired()) { - Client.close(Handle, code, reason); + websocketpp::lib::error_code ec; + Client.close(Handle, code, reason, ec); } Cleanup(); } @@ -390,7 +402,7 @@ void V8WebSocketClientImpl::OnFail(wspp_connection_hdl InHandle) // must not raise exception in js, recommend just push a pending msg and process later. Handles[ON_FAIL].Get(Isolate)->Call(GContext.Get(Isolate), v8::Undefined(Isolate), 1, args); } - Close(websocketpp::close::status::abnormal_close, ""); + CloseImmediately(websocketpp::close::status::abnormal_close, ""); } } // namespace PUERTS_NAMESPACE