gpt4 book ai didi

c# - Xamarin Mono ClientWebSocket 实现不适用于安全套接字

转载 作者:太空狗 更新时间:2023-10-29 22:00:09 24 4
gpt4 key购买 nike

我在 Osx 上实现安全的网络套接字客户端时遇到问题。我正在使用 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

最佳答案

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

关于c# - Xamarin Mono ClientWebSocket 实现不适用于安全套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com