Benedicht / BestHTTP-Issues

Issue tracking repo for the Best HTTP and all related Unity plugins.
https://assetstore.unity.com/publishers/4137
11 stars 1 forks source link

SignalR Core WebSocket WebGL issue #175

Closed Scormave closed 10 months ago

Scormave commented 11 months ago

Hello!

I'm trying to get SignalR Core working in WebGL build. When WebGL build is loaded, everything is fine and work as intended. But, when I close the Unity app without closing the tab (it is a case for a one-page web app for ex.), I encounter errors like this one:

Uncaught RuntimeError: memory access out of bounds
    at GC_reclaim_small_nonempty_block
    at GC_generic_malloc_inner
    at il2cpp::vm::Object::AllocateSpec(unsigned long, Il2CppClass*)
    at il2cpp::vm::Object::NewAllocSpecific(Il2CppClass*)
    at il2cpp::vm::Thread::SetupInternalManagedThread(Il2CppThread*, il2cpp::os::Thread*)
    at il2cpp::vm::Thread::Attach(Il2CppDomain*)
    at il2cpp::vm::ScopedThreadAttacher::ScopedThreadAttacher()
    at ReversePInvokeWrapper_WebGLBrowser_OnBinaryCallback_mFF091A0B29C51FAC284D69E475AF7BF1E151F313
    at dynCall_viii
    at Object.dynCall_viii
    at socket.socketImpl.onmessage

Even if I call StartClose for HubConnection in OnApplicationQuit nothing changes, there are still errors. It looks like JavaScript part of WebSocket implementation is trying to call the Unity part, but the Unity instance is already dead. Sometimes after these errors the whole tab freezes and can be killed only from Task Manager. Could you please help me with this issue?

Benedicht commented 11 months ago

I think StartClose doesn't work because it just starts the closing procedure, it doesn't tears down the whole connection immediately.

Benedicht commented 11 months ago

Could you try out this patched file: https://gist.github.com/Benedicht/2eb7c1e29fd5684de4cbd9340d2bdcb5 You have to overwrite the old one in the Best HTTP\Plugins\WebGL\ folder (BestHTTP_WebSocket.jslib).

Then you can add the following line somewhere:

#if UNITY_WEBGL && !UNITY_EDITOR
        [DllImport("__Internal")]
        public static extern void WS_EmergencyRelease();
#endif

And use it in your OnApplicationQuit event-handler:

#if UNITY_WEBGL && !UNITY_EDITOR
        WS_EmergencyRelease();
#endif
Scormave commented 11 months ago

I've tried the patched file and WS_EmergencyRelease call and it seems that everything works fine without any errors. Thank you! Will this fix be included in the next version of BestHTTP?

Benedicht commented 11 months ago

Will this fix be included in the next version of BestHTTP?

I'm not sure about it yet.

Benedicht commented 10 months ago

Will keep the changes in the .jslib, anyone who needs it just have to add the DllImport code:

#if UNITY_WEBGL && !UNITY_EDITOR
        [DllImport("__Internal")]
        public static extern void WS_EmergencyRelease();
#endif