There are plenty of good Websocket APIs for C#, but I run into trouble when targeting Unity WebGL due to this restriction described in the Unity documentation:
No direct socket access
Due to security implications, JavaScript code does not have direct access to IP Sockets to implement network connectivity. As a result, the .NET networking classes (ie, everything in the
System.Net
namespace, particularlySystem.Net.Sockets
) are non-functional in WebGL. The same applies to Unity’s oldUnityEngine.Network*
classes, which are not available when building for WebGL.If you need to use Networking in WebGL, you currently have the options to use the
WWW
orUnityWebRequest
classes in Unity or the new Unity Networking features which support WebGL, or to implement your own networking using WebSockets or WebRTC in JavaScript.
How can I work around this restriction for networking in my Unity WebGL game?