如何使用 unity 和 websockets

how to use unity and websockets

大家好,我正在使用 websocketsharp 构建 unity webGL,https://github.com/sta/websocket-sharp

奇怪的是,当我从客户端 运行 它工作正常时,我可以使用 nodeJS 发送和接收消息,但是当我将构建上传到我的服务器时,当我尝试连接到服务器

12/5/2021 6:53:40 PM|Fatal|.|System.Net.Sockets.SocketException (0x80004005):无法解析主机 'mydomain.io' Desktop.loader.js:1 在 System.Net.Dns.Error_11001 (System.String 主机名) [0x00000] 在 <00000000000000000000000000000000>:0

这是我在 C# 中的连接方式

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

我不知道如何调试它。为什么它在 unity 编辑器中有效,但在我上传到服务器时却无效? websocketssharp 是与 nodeJS 服务器通信的最佳方式吗?

据我了解一般 WebSocket 和网络 .Net / c# 类 不是 supported in WebGL。您需要在 JavaScript 端进行联网或使用 UnityWebRequest.


特别是来自您正在使用的链接资产

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?

-> 因为在 Unity 编辑器中,您仍然在完全支持这些 类 的独立 PC 上执行预编译的 .Net 代码。在您实际构建项目之前,它不会编译成 WebGL