Xamarin Mono客户端WebSocket实现不支持安全套接字。

6

我在Osx上实现安全Web Socket客户端时遇到了问题。我正在使用System.Net.WebSockets中的ClientWebSocket。这是一些测试代码:

    static async Task RunLoop()
    {
        ClientWebSocket ws = new ClientWebSocket();
        await ws.ConnectAsync(new Uri("wss://echo.websocket.org"), CancellationToken.None);

        do
        {
            Console.Write("Enter message:");
            var msg = Console.ReadLine();
            if (msg == "quit")
            {
                break;
            }

            var b = new ArraySegment<byte>(UTF8Encoding.UTF8.GetBytes(msg));
            await ws.SendAsync(b, WebSocketMessageType.Text, true, CancellationToken.None);

            byte[] bb = new byte[2048];
            ArraySegment<byte> buffer = new ArraySegment<byte>(bb);

            var result = await ws.ReceiveAsync(buffer, CancellationToken.None);

            switch (result.MessageType)
            {
                case WebSocketMessageType.Text:
                    Console.WriteLine(UTF8Encoding.UTF8.GetString(buffer.Array));
                    break;
                case WebSocketMessageType.Close:
                    await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                    break;
            }

        } while (true);
    }

    static void Main(string[] args)
    {
        var task = RunLoop();
        task.Wait();
    }

每次调用ReceiveAsync时,我都会收到这个异常。
Cannot access a disposed object.

对象名称:'System.Net.Sockets.NetworkStream'。

    at System.Net.WebConnection.Read (System.Net.HttpWebRequest request, System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x0001f] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System/System.Net/WebConnection.cs:1038 
  at System.Net.WebSockets.ClientWebSocket+<ReceiveAsync>c__AnonStorey5.<>m__0 () [0x0017c] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System/System.Net.WebSockets/ClientWebSocket.cs:259 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x00012] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:680 
  at System.Threading.Tasks.Task.Execute () [0x00016] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
      at Test.MainClass+<RunLoop>c__async2.MoveNext () [0x001c0] in Program.cs:81

似乎套接字被处理掉了,但我不知道为什么和在哪里。相同的代码对于非安全的websockets(“ws://echo.websocket.org”)正常工作。
也尝试过WebSocket.Portable实现,但几乎有相同的问题。对于非安全的websockets它正常工作,但是安全websockets的OnMessage事件从未被调用。 https://github.com/NVentimiglia/WebSocket.Portable

相同的代码在Windows上适用于wss。 - Ionut Holbia
请求后,服务器端会出现警报。 第9852帧:线上103字节(824位),接口0捕获103字节(824位), 以太网II协议,源地址:Solarfla_07:de:fc(00:0f:53:07:de:fc),目的地址:Apple_aa:05:d8(68:5b:35:aa:05:d8) IPv4协议,源地址:54.77.29.155,目的地址:10.40.131.187 传输控制协议,源端口443,目的端口52146,序列号5121,确认号1038,长度37 安全套接层     TLSv1记录层:加密警报         内容类型:警报(21)         版本:TLS 1.0(0x0301)         长度:32         警报消息:加密警报 - Ionut Holbia
1
你成功找到解决问题的方法了吗?我遇到了类似的问题,只能使用非安全协议连接,不确定要为我的Xamarin应用程序选择哪种WebSocket基础架构。 - Cosmin
你好,这个有任何更新吗? - Piyush Aghera
1个回答

0

我找到了一个解决这个问题的方法。我使用了SocketRocker,它是一个Objective C实现。Square.SocketRocket是原生Objective C库的Xamarin包装器。 在GitHub上,您还可以在示例目录中找到一些示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接