UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[Transport] Fix WebGL Secure WebSocket WSS Connection Failures

Solution

networkingwebglproject architecturewebsocketsmultiplayer

Unity 2021.3.x - Unity 6.3.x

Published Mon, May 4

Issue

 A persistent issue is observed when attempting to establish secure WebSocket (wss://) connections in WebGL builds. While insecure connections function correctly, secure connections consistently fail with a browser-reported error. This occurs because NetworkSettings erroneously attempts to connect to port 0 when switching to the WSS protocol, regardless of the valid TLS certificates present on the server.

Explanation

The failures involving secure WebSocket connections in WebGL using the Unity Transport package are primarily rooted in a logic error during the handshake phase. This specific bug, which caused the client to target port 0 instead of the specified secure port, was officially addressed in version 2.5.3 of the package.

To resolve this issue, follow these steps:

  1. Open the Package Manager and update Unity Transport to version 2.5.3 or a later version.
  2. Ensure your NetworkSettings are initialized to support secure parameters.
  3. Implement the WithSecureClientParameters method when configuring your NetworkSettings to trigger the secure pathway.

Additional Tips

  • Browsers handle the TLS certificate validation in WebGL; ensure your server’s certificate is valid and not self-signed unless the certificate is manually trusted by the browser.
  • Verify that your server is configured to accept the sec-websocket-protocol if your NetworkSettings specify a sub-protocol.
  • If connection issues persist, check that the server is not blocking the specific port via a firewall, as WebGL connections are subject to standard browser CORS and security policies.

TL;DR

Secure WebSocket connection failures in WebGL are resolved by updating the Unity Transport package to version 2.5.3 or newer to fix a NetworkSettings port 0 mapping error.


Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.