TcpThread seems to no longer work in 2019.4

Hello,

I have been sifting through the forums and various other places and I am not seeing any mention of this. So either this is a new bug or something that is specific to me!

Ultimately, when running this code in WebGL in version 2019.4.13f1:

            TcpClient client = new TcpClient("google.com", 80);
            Debug.Log(client);
           
            client.Client.Close();
            client.Close();

An exception will be generated: “Could not resolve host ‘google.com’”

Note that no matter what host I try, all hosts seem to fail. If I run the above in the editor or any other unity platform (tested windows/linux/android), it works fine. So I don’t think it’s an issue with my own DNS client.

Also, if I avoid the dns lookup by doing something like:

            string sip = "136.244.94.132";
           
            TcpClient client = new TcpClient();
            client.Client.Connect(IPAddress.Parse(sip), uri.Port);
            Debug.Log(client);
           
            client.Client.Close();
            client.Close();

I get a strange result, where an exception is thrown, but the message of the exception is simply “Success”. Despite the exception seemingly reporting some sort of success, an exception is being thrown, meaning that the TcpClient is not usable in either case.

This is used within the websocket-sharp library in order to create the websocket connection, and it has definitely worked in previous versions of Unity (I’m not 100% sure which version, but it was definitely a fairly recent version), so I assume that this is an issue that is specific to 2019.4?

Thanks in advance for any help!

Are you using this library? GitHub - sta/websocket-sharp: A C# implementation of the WebSocket protocol client and server

If so, if I understand correctly, it is not intended to be used from a C# program compiled to WebAssembly, but it implements a C# WebSocket API on top of native BSD/POSIX Sockets API. On the web there is no access to BSD/POSIX Sockets API, but browsers provide WebSockets API in JavaScript. So a library should implement C# WebSockets access on top of JS WebSockets instead.

In a web browser it is not possible to resolve domain names to IP addresses, or to reverse resolve IP addresses to domain names. That is beyond the security capability of WebSockets and other web APIs.

Although when you write that is a regression, I am not sure how to react to that. My impression is that what you are trying should not work in any version of Unity in a web browser, because of web security limitations. Which Unity version did it (seem to?) work for you? Maybe something else has changed around in the configuration?

Thank you Jukka for your detailed answer!

I think that you might be right. I have used the websocket-sharp library for a few different projects, but it was mostly embedded into other plugins where as this time I am using it on it’s own (essentially to try and replace unet for an existing project).

I understand what it is that you are suggesting to do, which is to essentially implement external methods that call native javascript websocket functions. However, the idea of that is a bit daunting! Do you know of any existing libraries that attempt to solve the above problem, or any documentation that could point me in the right direction?

Many thanks!

Sorry, I am not currently aware of any such libraries.

To interact with JS code, check out Unity - Manual: Interaction with browser scripting that explains how to do JS ↔ C/C++/C# interop.