Skip to content

Commit

Permalink
[unity][unreal]当readyState为OPEN时,通过ping去测试下连接状态,防止因为没切后台之类的操作导致没及时更新j…
Browse files Browse the repository at this point in the history
…s侧状态而错误的发送消息。
  • Loading branch information
chexiongsheng committed Nov 27, 2024
1 parent cca7bcb commit 38a4284
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class WebSocket extends EventTarget {
}

get readyState() {
if (this._readyState === WebSocket.OPEN) {
const [statue, message] = this._raw.statue();
if (statue != 0) {
this._fail(`${message}[${statue}]`);
}
}
return this._readyState;
}

Expand Down
3 changes: 3 additions & 0 deletions unity/test/Src/Cases/WebsocketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public async Task SmokeTest()
con.addEventListener('open', (ev) => {
console.log(`on open`);
con.send('puerts websocket');
if (con.readyState != WebSocket.OPEN) {
throw new Error('invalid readyState');
}
});
con.addEventListener('message', (ev) => {
console.log(`on message: ${ev.data}`);
Expand Down
23 changes: 23 additions & 0 deletions unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class V8WebSocketClientImpl

void Close(const v8::FunctionCallbackInfo<v8::Value>& Info);

void Statue(const v8::FunctionCallbackInfo<v8::Value>& Info);

void CloseImmediately(websocketpp::close::status::value const code, std::string const& reason);

void PollOne();
Expand Down Expand Up @@ -309,6 +311,20 @@ void V8WebSocketClientImpl::Close(const v8::FunctionCallbackInfo<v8::Value>& Inf
Cleanup();
}

void V8WebSocketClientImpl::Statue(const v8::FunctionCallbackInfo<v8::Value>& Info)
{
websocketpp::lib::error_code ec;
Client.ping(Handle, "", ec);
auto isolate = Info.GetIsolate();
auto context = isolate->GetCurrentContext();
auto res = v8::Array::New(isolate);

res->Set(context, 0, v8::Int32::New(isolate, ec.value())).Check();
res->Set(context, 1,
v8::String::NewFromUtf8(isolate, ec.message().c_str(), v8::NewStringType::kNormal, ec.message().size()).ToLocalChecked());
Info.GetReturnValue().Set(res);
}

void V8WebSocketClientImpl::CloseImmediately(websocketpp::close::status::value const code, std::string const& reason)
{
if (!Handle.expired())
Expand Down Expand Up @@ -444,6 +460,13 @@ void InitWebsocketPPWrap(v8::Local<v8::Context> Context)
->Close(Info);
}));

WSTemplate->PrototypeTemplate()->Set(v8::String::NewFromUtf8(Isolate, "statue").ToLocalChecked(),
v8::FunctionTemplate::New(Isolate,
[](const v8::FunctionCallbackInfo<v8::Value>& Info) {
static_cast<PUERTS_NAMESPACE::V8WebSocketClientImpl*>(Info.Holder()->GetAlignedPointerFromInternalField(0))
->Statue(Info);
}));

WSTemplate->PrototypeTemplate()->Set(v8::String::NewFromUtf8(Isolate, "poll").ToLocalChecked(),
v8::FunctionTemplate::New(Isolate,
[](const v8::FunctionCallbackInfo<v8::Value>& Info)
Expand Down

0 comments on commit 38a4284

Please sign in to comment.