Skip to content

Commit

Permalink
[unity]set_verify_mode和close用无异常的版本,并转为js异常,然后在js侧转为事件
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Nov 26, 2024
1 parent 0c886f8 commit 63aca75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
26 changes: 19 additions & 7 deletions unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class V8WebSocketClientImpl

void Close(const v8::FunctionCallbackInfo<v8::Value>& 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();

Expand Down Expand Up @@ -143,7 +143,7 @@ class V8WebSocketClientImpl
static void OnGarbageCollectedWithFree(const v8::WeakCallbackInfo<V8WebSocketClientImpl>& 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();
}

Expand All @@ -158,7 +158,9 @@ V8WebSocketClientImpl::V8WebSocketClientImpl(v8::Isolate* InIsolate, v8::Local<v
websocketpp::lib::shared_ptr<puerts_asio::ssl::context> on_tls_init(websocketpp::connection_hdl)
{
auto ctx = websocketpp::lib::make_shared<puerts_asio::ssl::context>(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
Expand Down Expand Up @@ -293,14 +295,24 @@ void V8WebSocketClientImpl::Close(const v8::FunctionCallbackInfo<v8::Value>& 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();
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 63aca75

Please sign in to comment.