我在Osx上实现安全的Web套接字客户端时遇到问题。 我正在使用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.
对象名称:&#39; System.Net.Sockets.NetworkStream&#39;。
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(&#34; ws://echo.websocket.org")。
尝试了WebSocket.Portable实现,但几乎有同样的问题。对于非安全的websockets,它工作正常,但对于安全的websockets,从未调用OnMessage事件。 Why is volatile needed in C?
答案 0 :(得分:0)
我找到了解决此问题的方法。我使用SocketRocker这是一个Objective C实现。 Square.SocketRocket是本机Objective C库的Xamarin包装器。 在GitHub上,您还可以在示例目录中找到一些示例。