Home > Mobile >  how to use unity and websockets
how to use unity and websockets

Time:12-07

Hey guys I'm making a unity webGL build with websocketsharp, https://github.com/sta/websocket-sharp

Weird thing is it works fine when I run it from client, I can send and receive messages with nodeJS but when I upload the build to my server I get this error when I try to connect to the server

12/5/2021 6:53:40 PM|Fatal|.|System.Net.Sockets.SocketException (0x80004005): Could not resolve host 'mydomain.io' Desktop.loader.js:1 at System.Net.Dns.Error_11001 (System.String hostName) [0x00000] in <00000000000000000000000000000000>:0

here is how I am connecting in C#

ws = new WebSocket("wss://mydomain.io:8000"); ws.SetProxy("https://mydomain.io/server", "", "");

I have no idea how to debug this. Why would it work in the unity editor but not when I upload to the server? Is websocketssharp the best way to communicate with a nodeJS server?

CodePudding user response:

As far as I understand in general WebSocket and networking .Net / c# classes are not supported in WebGL. You would need to either do the networking on JavaScript side or use UnityWebRequest.


In particular from the linked asset you are using

It works with Unity Free, but there are some limitations:

  • Security Sandbox of the Webplayer (The server is not available in Web Player)
  • WebGL Networking (Not available in WebGL)

Why would it work in the unity editor but not when I upload to the server?

-> Because within the Unity Editor you are still executing the pre-compiled .Net code on your standalone PC where these classes are fully supported. It isn't compiled into WebGL until you actually build your project.

  • Related