使用带有Unity的WebSocket时无法识别URI前缀

时间:2017-06-05 10:48:03

标签: c# macos unity3d websocket

using System.Net.WebSockets;
using System.Threading.Tasks;
using System.Threading;

public class Server : MonoBehaviour
{
    private string uri = "ws://localhost:4567";
    private ClientWebSocket ws;

    void Start()
    {
        ws = new ClientWebSocket();
        ws.ConnectAsync(new Uri(uri), CancellationToken.None).Wait();
    }
}

我正在使用Unity版2017.1.0b7 Personal。

我转到Edit -> Project Settings -> Player -> Inspector -> Other Settings -> Configuration并将Scripting Runtime Version设置为Experimental (.NET 4.6 Equivalent)API Compatibility Level已成为.NET 4.6。 之后,上面的代码在IDE中的内置Unity播放器中运行良好。它确实连接到我的localhost-ed websocket服务器。

但是,如果我之后进行独立构建并使用游戏创建.app,则上面的代码会产生错误:

OSXPlayer(<hostname>)</i> NotSupportedException: The URI prefix is not recognized.
  at System.Net.WebRequest.Create (System.Uri requestUri, System.Boolean useUriBase) [0x000db] in <f044fe2c9e7e4b8e91984b41f0cf0b04>:0 

为什么会这样?我该如何解决? 谢谢。

1 个答案:

答案 0 :(得分:0)

应在将来的版本2017.3.0b1中修复。

转载:2017.1.0p2,2017.2.0b5,2017.3.3a2

向后移植到2017.1.1p2,2017.2.0b11 修正在:2017.3.0b1

https://issuetracker.unity3d.com/issues/dot-net-4-dot-6-ios-build-throws-notsupportedexception-when-creating-a-webrequest-via-system-dot-net-dot-webrequest-dot-create