Unity3D and websockets

I have been trying to find info on websockets in unity3d, but everywhere I can find a mention of it on the unity3D docs are dead links, or it says it is deprecated, without giving an alternative, so am wondering if any good Samaritans out there could give me an overview on current websocket support in Unity3D? :slight_smile:

I would like to be able to use the standard browser’s window.WebSocket in html5, and whatever is available in android and iOS

Hey @zukias I can help. Most of the WebSocket libraries have been deprecated or have become dead links because support for .NET4.6 in Unity enables the use of the standard library WebSocket implementation. This does not work on HTML5 builds though so you’ll need a small shim for that build target.

We don’t use the System.Net.WebSocket namespace in Nakama for a few different reasons (all are due to bugs in Unity):

  • IPv4 address is not tried correctly if the IPv6 address fails to resolve; it’s a problem in TcpClient.
  • The WebSocket will always fail to respond to frame control messages (i.e. PINGs) after 300 seconds.

Instead we use this reimplementation of the standard library WebSockets as a standalone library:

https://github.com/ninjasource/Ninja.WebSockets

You can find the small shim we use for HTML5 builds on our open-source code repo:

https://github.com/heroiclabs/nakama-unity/blob/master/Assets/Nakama/Plugins/UnityWebGLSocketBridge.jslib

Hope this helps.