我正在尝试在Unity 2017.4.10f1中使用ClientWebSocket
类,并且得到了ObjectDisposedException
。
这是我正在使用的代码:
private async void ContinousReceiveAsync()
{
Debug.Log("Receiving...");
while (State == WebSocketState.Open)
{
List<byte> _buffer = new List<byte>();
WebSocketReceiveResult result = null;
do
{
ArraySegment<byte> _segment = new ArraySegment<byte>(new byte[1024]);
result = await _socket.ReceiveAsync(_segment, CancellationToken.None);
switch (result.MessageType)
{
case WebSocketMessageType.Text:
_buffer.AddRange(_segment.Array);
if(result.EndOfMessage)
TextRecieved(Encoding.UTF8.GetString(_buffer.ToArray()));
break;
case WebSocketMessageType.Binary:
byte[] _data = new byte[result.Count];
Array.Copy(_segment.Array, _data, result.Count);
_buffer.AddRange(_data);
if (result.EndOfMessage)
BinaryRecieved(_buffer.ToArray());
break;
case WebSocketMessageType.Close:
SocketCleanUp(result?.CloseStatus);
if (AutoReconnect)
{
Reconnect();
}
break;
default:
Debug.LogError("[Madapter.Websocket] Unknown WebSocketMessageType");
break;
}
}
while (!result.EndOfMessage);
}
Debug.Log("Stopped Receiving");
}
ReceiveAsync
抛出对象System.Net.Sockets.NetworkStream的ObjectDisposedException
。当我尝试通过websocket发送邮件时,这种情况只会立即发生。
这是Send
函数:
public async Task SendTextAsync(string msg)
{
if(State == WebSocketState.Open)
{
ArraySegment<byte> buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg));
await SendAsync(buffer, WebSocketMessageType.Text);
}
}
public async Task SendBinaryAsync(byte[] data)
{
if (State == WebSocketState.Open)
{
ArraySegment<byte> buffer = new ArraySegment<byte>(data);
await SendAsync(buffer, WebSocketMessageType.Binary);
}
}
private async Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType type)
{
await _socket.SendAsync(buffer, type, true, CancellationToken.None);
}
错误:
ObjectDisposedException:无法访问已处置的对象。
对象名称:“ System.Net.Sockets.NetworkStream”。
System.Net.WebConnection.Read(System.Net.HttpWebRequest请求,System.Byte []缓冲区,System.Int32偏移量,System.Int32大小)(为:0) System.Net.WebSockets.ClientWebSocket + <> c__DisplayClass35_0.b__0()(在:0处) System.Threading.Tasks.Task1[TResult].InnerInvoke () (at <cfc149f8218b496788d8493c87de777a>:0) System.Threading.Tasks.Task.Execute () (at <cfc149f8218b496788d8493c87de777a>:0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <cfc149f8218b496788d8493c87de777a>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <cfc149f8218b496788d8493c87de777a>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <cfc149f8218b496788d8493c87de777a>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <cfc149f8218b496788d8493c87de777a>:0) System.Runtime.CompilerServices.TaskAwaiter
1 [TResult] .GetResult()(在:0处) Madness.Madapter.WebSocket + c__async5.MoveNext()(在资产/ MAD工具/ Madapter / WebSocket.cs:98) ---从之前引发异常的位置开始的堆栈结束跟踪--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()(在:0处) System.Runtime.CompilerServices.AsyncMethodBuilderCore + <> c.b__6_0(System.Object状态)(位于:0) UnityEngine.UnitySynchronizationContext + WorkRequest.Invoke()(在C:/buildslave/unity/build/Runtime/Export/UnitySynchronizationContext.cs:92) UnityEngine.UnitySynchronizationContext.Exec()(在C:/buildslave/unity/build/Runtime/Export/UnitySynchronizationContext.cs:53) UnityEngine.UnitySynchronizationContext.ExecuteTasks()(在C:/buildslave/unity/build/Runtime/Export/UnitySynchronizationContext.cs:74)