MirrorNetworking / Mirror

#1 Open Source Unity Networking Library
https://mirror-networking.com
MIT License
5.21k stars 769 forks source link

Websocket ClientConnected() causes an error on WebGL #1486

Closed MaZyGer closed 3 years ago

MaZyGer commented 4 years ago

Describe the bug I use two websocket transport for different clients with different ports. The one is for networkmanager (like you would use in normal case with mirror) and other for my custom matchmaking server (which has only list of server information like the steam server browser) which uses websockettransport. It works well on PC. I can start the gameserver and the matchmaking server they are communicating.

If I use transport.ClientConnected() with WebGL on a browser it cause an error. I am not sure if this is caused because I use two WebsocketTransport. On PC version I have no problem with them. The error

exception thrown: TypeError: Cannot read property 'readyState' of undefined,TypeError: Cannot read property 'readyState' of undefined
    at _SocketState (blob:http://localhost/f1a388d0-2f74-4ed6-8fb7-df8e661338bb:3949:16)

After trying around I found out this causes the error: https://github.com/vis2k/Mirror/blob/90d92fc0cf7b1c24613c27393ef87ab9c7cec879/Assets/Mirror/Runtime/Transport/Websocket/ClientJs.cs#L36

The error looks similair.

To Reproduce Steps to reproduce the behavior: I am not sure if this caused because I use two websockettransport but you can reproduce it even without to connect somewhere.

  1. Have a gameobject with NetworkManager and WebsocketTransport.
  2. Have a WebsocketTransport and use different port and a custom script to use transport.ClientConnected() in OnGUI() with if condition. Optional you can use Buttons
  3. Build it as WebGL version and run try it.

Expected behavior I excepted to get false or true if the websocket is connected or not. like ClientConnected() always do.

Desktop (please complete the following information):

miwarnec commented 4 years ago

@MaZyGer does this work with Unity 2018.3.6f1?

MaZyGer commented 4 years ago

I just tried it, I get same error there. I tried with chrome and firefox. For some reason firefox says socket is undefined and chrome Cannot read property 'readyState' of undefined

Here is the full output of chrome and firefox console:

Chrome

UnityLoader.js:3 You can reduce your startup time if you configure your web server to host .unityweb files using gzip compression.
UnityLoader.js:4 [UnityCache] 'http://localhost:56653/Build/Build.wasm.code.unityweb' successfully downloaded and stored in the indexedDB cache
UnityLoader.js:4 [UnityCache] 'http://localhost:56653/Build/Build.wasm.framework.unityweb' successfully downloaded and stored in the indexedDB cache
UnityLoader.js:4 [UnityCache] 'http://localhost:56653/Build/Build.data.unityweb' successfully downloaded and stored in the indexedDB cache
UnityLoader.js:4 Loading player data from data.unity3d
UnityLoader.js:4 Initialize engine version: 2018.3.6f1 (a220877bc173)
UnityLoader.js:4 Creating WebGL 2.0 context.
UnityLoader.js:4 Renderer: WebKit WebGL
UnityLoader.js:4 Vendor:   WebKit
UnityLoader.js:4 Version:  OpenGL ES 3.0 (WebGL 2.0 (OpenGL ES 3.0 Chromium))
UnityLoader.js:4 GLES:     3
UnityLoader.js:4  EXT_color_buffer_float GL_EXT_color_buffer_float EXT_disjoint_timer_query_webgl2 GL_EXT_disjoint_timer_query_webgl2 EXT_float_blend GL_EXT_float_blend EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic KHR_parallel_shader_compile GL_KHR_parallel_shader_compile OES_texture_float_linear GL_OES_texture_float_linear WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_compressed_texture_s3tc_srgb GL_WEBGL_compressed_texture_s3tc_srgb WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_lose_context GL_WEBGL_lose_context OVR_multiview2 GL_OVR_multiview2
UnityLoader.js:4 OPENGL LOG: Creating OpenGL ES 3.0 graphics device ; Context level  <OpenGL ES 3.0> ; Context handle 1
8The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
UnityLoader.js:4 Websocket transport initialized!
UnityLoader.js:4  
UnityLoader.js:4 (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
UnityLoader.js:4 
UnityLoader.js:4 UnloadTime: 0.435000 ms
UnityLoader.js:4 exception thrown: TypeError: Cannot read property 'readyState' of undefined,TypeError: Cannot read property 'readyState' of undefined
    at _SocketState (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:185685)
    at wasm-function[26058]:0x972d43
    at wasm-function[26126]:0x97735f
    at wasm-function[22085]:0x8b993a
    at wasm-function[43775]:0xdf3f59
    at wasm-function[43774]:0xdf36a5
    at wasm-function[20449]:0x893608
    at wasm-function[47180]:0xe8deaa
    at Object.dynCall_iiiii (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:478656)
    at invoke_iiiii (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:365263)
    at wasm-function[45049]:0xe2eb37
    at wasm-function[45462]:0xe3f6cc
    at wasm-function[3060]:0x18129e
    at wasm-function[3059]:0x18120a
    at wasm-function[11582]:0x4d1299
    at wasm-function[11589]:0x4d16f3
    at wasm-function[8049]:0x351613
    at wasm-function[11548]:0x4cd015
    at wasm-function[11552]:0x4cd61c
    at wasm-function[11546]:0x4cc96b
    at wasm-function[11540]:0x4cbdcc
    at wasm-function[8862]:0x3a5eb0
    at wasm-function[8823]:0x3a39f6
    at wasm-function[8149]:0x35ab2f
    at wasm-function[8149]:0x35ab44
    at wasm-function[8143]:0x359ef4
    at wasm-function[8138]:0x358824
    at wasm-function[47194]:0xe8e0dc
    at Object.dynCall_v (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:487119)
    at browserIterationFunc (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:131556)
    at Object.runIter (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:134629)
    at Browser_mainLoop_runner (blob:http://localhost:56653/50c75041-3e5f-4afe-974d-3a1779ebcb95:2:133091)
printErr @ UnityLoader.js:4
UnityLoader.js:3 Invoking error handler due to
Uncaught TypeError: Cannot read property 'readyState' of undefined
50c75041-3e5f-4afe-974d-3a1779ebcb95:2 Uncaught TypeError: Cannot read property 'readyState' of undefined
    at _SocketState (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
    at :56653/wasm-function[26058]:0x972d43
    at :56653/wasm-function[26126]:0x97735f
    at :56653/wasm-function[22085]:0x8b993a
    at :56653/wasm-function[43775]:0xdf3f59
    at :56653/wasm-function[43774]:0xdf36a5
    at :56653/wasm-function[20449]:0x893608
    at :56653/wasm-function[47180]:0xe8deaa
    at Object.dynCall_iiiii (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
    at invoke_iiiii (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
    at :56653/wasm-function[45049]:0xe2eb37
    at :56653/wasm-function[45462]:0xe3f6cc
    at :56653/wasm-function[3060]:0x18129e
    at :56653/wasm-function[3059]:0x18120a
    at :56653/wasm-function[11582]:0x4d1299
    at :56653/wasm-function[11589]:0x4d16f3
    at :56653/wasm-function[8049]:0x351613
    at :56653/wasm-function[11548]:0x4cd015
    at :56653/wasm-function[11552]:0x4cd61c
    at :56653/wasm-function[11546]:0x4cc96b
    at :56653/wasm-function[11540]:0x4cbdcc
    at :56653/wasm-function[8862]:0x3a5eb0
    at :56653/wasm-function[8823]:0x3a39f6
    at :56653/wasm-function[8149]:0x35ab2f
    at :56653/wasm-function[8149]:0x35ab44
    at :56653/wasm-function[8143]:0x359ef4
    at :56653/wasm-function[8138]:0x358824
    at :56653/wasm-function[47194]:0xe8e0dc
    at Object.dynCall_v (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
    at browserIterationFunc (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
    at Object.runIter (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
    at Browser_mainLoop_runner (50c75041-3e5f-4afe-974d-3a1779ebcb95:2)
50c75041-3e5f-4afe-974d-3a1779ebcb95:2 warning: 2 FS.syncfs operations in flight at once, probably just doing extra work

Firefox

Das Skript von "http://localhost:56653/TemplateData/UnityProgress.js" wurde geladen, obwohl sein MIME-Typ ("") kein für JavaScript gültiger MIME-Typ ist. localhost:56653
Das Skript von "http://localhost:56653/Build/UnityLoader.js" wurde geladen, obwohl sein MIME-Typ ("") kein für JavaScript gültiger MIME-Typ ist. localhost:56653
Successfully compiled asm.js code (total compilation time 0ms) 3 UnityLoader.js
You can reduce your startup time if you configure your web server to host .unityweb files using gzip compression. UnityLoader.js:3:8488
[UnityCache] 'http://localhost:56653/Build/Build.wasm.code.unityweb' successfully downloaded and stored in the indexedDB cache UnityLoader.js:4:11222
[UnityCache] 'http://localhost:56653/Build/Build.wasm.framework.unityweb' successfully downloaded and stored in the indexedDB cache UnityLoader.js:4:11222
[UnityCache] 'http://localhost:56653/Build/Build.data.unityweb' successfully downloaded and stored in the indexedDB cache UnityLoader.js:4:11222
Der Bewegungssensor sollte nicht mehr verwendet werden. da409524-cd28-40ab-8ecb-cc708bc6409b:2:236141
Der Orientierungssensor sollte nicht mehr verwendet werden. da409524-cd28-40ab-8ecb-cc708bc6409b:2:236141
Loading player data from data.unity3d UnityLoader.js:4:9388
Initialize engine version: 2018.3.6f1 (a220877bc173) UnityLoader.js:4:9388
Creating WebGL 2.0 context. UnityLoader.js:4:9388
Renderer: Mozilla UnityLoader.js:4:9388
Vendor:   Mozilla UnityLoader.js:4:9388
Version:  OpenGL ES 3.0 (WebGL 2.0) UnityLoader.js:4:9388
GLES:     3 UnityLoader.js:4:9388
 EXT_color_buffer_float GL_EXT_color_buffer_float EXT_float_blend GL_EXT_float_blend EXT_texture_compression_bptc GL_EXT_texture_compression_bptc EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic OES_texture_float_linear GL_OES_texture_float_linear OVR_multiview2 GL_OVR_multiview2 WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_compressed_texture_s3tc_srgb GL_WEBGL_compressed_texture_s3tc_srgb WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_lose_context GL_WEBGL_lose_context UnityLoader.js:4:9388
OPENGL LOG: Creating OpenGL ES 3.0 graphics device ; Context level  <OpenGL ES 3.0> ; Context handle 1 UnityLoader.js:4:9388
Websocket transport initialized! UnityLoader.js:4:9388
 UnityLoader.js:4:9388
(Filename: ./Runtime/Export/Debug.bindings.h Line: 45) UnityLoader.js:4:9388
<empty string> UnityLoader.js:4:9388
UnloadTime: 1.000000 ms UnityLoader.js:4:9388
exception thrown: TypeError: socket is undefined,_SocketState@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:185671
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[26058]:0x972d43
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[26126]:0x97735f
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[22085]:0x8b993a
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[43775]:0xdf3f59
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[43774]:0xdf36a5
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[20449]:0x893608
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[47180]:0xe8deaa
UnityLoader["28fb832430d3ac4eef4ce353386c459c"]/Module.dynCall_iiiii@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:478656
invoke_iiiii@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:365247
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[45049]:0xe2eb37
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[45462]:0xe3f6cc
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[3060]:0x18129e
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[3059]:0x18120a
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[11582]:0x4d1299
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[11589]:0x4d16f3
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8049]:0x351613
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[11548]:0x4cd015
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[11552]:0x4cd61c
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[11546]:0x4cc96b
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[11540]:0x4cbdcc
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8862]:0x3a5eb0
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8823]:0x3a39f6
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8149]:0x35ab2f
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8149]:0x35ab44
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8143]:0x359ef4
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[8138]:0x358824
@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:wasm-function[47194]:0xe8e0dc
UnityLoader["28fb832430d3ac4eef4ce353386c459c"]/Module.dynCall_v@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:487119
browserIterationFunc@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:131544
runIter@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:134629
Browser_mainLoop_runner@blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2:133091
UnityLoader.js:4:9425
Invoking error handler due to
TypeError: socket is undefined UnityLoader.js:3:16887
TypeError: socket is undefined da409524-cd28-40ab-8ecb-cc708bc6409b:2:185671
    _SocketState blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:9907523
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:9925471
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:9148730
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:14630745
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:14628517
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:8992264
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:15261354
    dynCall_iiiii blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
    invoke_iiiii blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:14871351
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:14939852
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:1577630
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:1577482
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:5051033
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:5052147
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3479059
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:5034005
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:5035548
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:5032299
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:5029324
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3825328
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3815926
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3517231
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3517252
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3514100
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:3508260
    <anonym> blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b line 2 > WebAssembly.instantiate:15261916
    dynCall_v blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
    browserIterationFunc blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
    runIter blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
    Browser_mainLoop_runner blob:http://localhost:56653/da409524-cd28-40ab-8ecb-cc708bc6409b:2
ChristianTucker commented 4 years ago

@MaZyGer

Using the current asset-store version of mirror [11.4.2 at the time of writing] I was not able to reproduce this issue based on your reproduction steps. Here's the steps I took to attempt to reproduce your issue.

I created a GameObject that had a NetworkManager and WebsocketTansport. I then added an additional WebsocketTransport that used a different port to that object and created a script that referenced the additional transport component. In this script I either called ServerStart() or ClientConnect(host) on the transport, depending on the build. Inside the OnGUI function, which I wrapped in a if(transport.isActiveAndEnabled) I tried a few use cases of transport.ClientConnected() which returned appropriate values in Chrome, Firefox, Brave, and Edge without errors. I was also able to use both transports normally.

If you're still experiencing this issue, could you please submit a reproduction repository to be looked at? Thanks.

paulpach commented 4 years ago

Can you provide a repro project?

MaZyGer commented 4 years ago

@ChristianTucker I just tested it with 2019.3.4f1 und Mirror 11.4.2. I have same error. Here you can test it: https://vmanager4008.premium-vserver.net/unity/matchmakingservice/
You can download the webgl package here and test locally if you want: https://vmanager4008.premium-vserver.net/unity/matchmakingservice/WebGL.zip

@paulpach I tested it with empty project when I wrote this above but do not find it anymore. I can remake it and provide soon. (Uploaded now, see below)

But anyways I made screenshots how I it looks in the current (inactive) project (like the empty project). Here I just added two websockets on different gameobjects and one drawui which uses the second websocket to check if it is connected. This was tested and you see the result above in the webgl url.

As you see, it is just few drag an drops and small script.

I will try once with with websocket1 (image) and use it in the drawui and see what happens. And will write result soon.

Edit: I made new empty project and tried there. Same errror. Project Cef.zip

MaZyGer commented 4 years ago

EDIT: I found the problem and the fix, see below Small update. It seems like it happens even with one WebsocketTransport. I am testing around. In the first post I showed the line: https://github.com/vis2k/Mirror/blob/90d92fc0cf7b1c24613c27393ef87ab9c7cec879/Assets/Mirror/Runtime/Transport/Websocket/ClientJs.cs#L36

I changed it to

nativeRef > 0 && SocketState(nativeRef) != 0 This works but its bad solution. Because if I connect so the nativeRef is still zero. After the next connection it is increased by 1.

My OnGUI looks now like so:

private void OnGUI()
{
    if(transport.isActiveAndEnabled)
    {
        if(GUILayout.Button("Test ClientConnected"))
        {
            result = $"Test completed: ClientConnected = {transport.ClientConnected()}";
            if(Application.isEditor)
            {
                result += $", ServerActive = {transport.ServerActive()}";
            }
        }

        if(GUILayout.Button("Try to connect"))
        {
            transport.ClientConnect("127.0.0.1");
        }

        if(Application.isEditor)
        {
            if(GUILayout.Button("Host"))
            {
                transport.ServerStart();
            }
        }

        GUILayout.Label(result);
    }
}

isActiveAndEnabled was mentions before so I thought I add it too to see differents. But now I can test little bit. In WebGL with original code (the ClientJS) if I press Test ClientConnected, it instantly crash and says the error like above (no property readyState...)

Found here the problem: Ok I added now some debug information to WebSocket.jslib and found out there the SocketState is the problem https://github.com/vis2k/Mirror/blob/abf95651e224b6bc73061a005482536a08d3b02c/Assets/Mirror/Runtime/Transport/Websocket/Plugins/WebSocket.jslib#L90 As you see there is no check if socket is undefined and not. I my case the websocketInstances is empty.. so there is no instances and readyState cannot be read.

Changing to this it fixed it.

        var socket = webSocketInstances[socketInstance];
        if(socket)
        {
            return socket.readyState;
        }
        return false;

But the question is, why I have only this problem or it is common problem but ClientConnected was never used? Here an image if I output with console.log before it was connected and after I connected to the server https://i.imgur.com/L53ZEFL.png

James-Frowen commented 3 years ago

Closing because there is a new websocket Transport

Repo: https://github.com/MirrorNetworking/SimpleWebTransport Folder in mirror: https://github.com/vis2k/Mirror/tree/master/Assets/Mirror/Runtime/Transport/SimpleWebTransport