Websocket TcpSocketClient—当我尝试读取尚未到达的任务内部的响应时,出现未处理异常
我使用此库()与TCP服务器通信,该服务器在生成事件时向我发送,然后,我必须始终验证TCP服务器是否向我发送了事件,但如果我在TCP服务器发送之前尝试读取任何内容,则会抛出未处理的异常,但只有在任务内部读取时才会发生,在主线程中,它抛出了一个超时异常,这是我在任务中预期会发生的异常。Websocket TcpSocketClient—当我尝试读取尚未到达的任务内部的响应时,出现未处理异常,websocket,xamarin.forms,unhandled-exception,timeoutexception,Websocket,Xamarin.forms,Unhandled Exception,Timeoutexception,我使用此库()与TCP服务器通信,该服务器在生成事件时向我发送,然后,我必须始终验证TCP服务器是否向我发送了事件,但如果我在TCP服务器发送之前尝试读取任何内容,则会抛出未处理的异常,但只有在任务内部读取时才会发生,在主线程中,它抛出了一个超时异常,这是我在任务中预期会发生的异常。 有人能帮我吗?谢谢下面是我的代码 public class CentralTcpService { #region ConnectTcpAsync public async void Connect
有人能帮我吗?谢谢下面是我的代码
public class CentralTcpService
{
#region ConnectTcpAsync
public async void ConnectTcpAsync()
{
try
{
_sockecClient = new TcpSocketClient();
await _sockecClient.ConnectAsync(Central.Ip, Central.Port);
_writter = new ExtendedBinaryWriter(_sockecClient.WriteStream);
_reader = new ExtendedBinaryReader(_sockecClient.ReadStream);
_writter.WriteString(EvenNotProtocol.MobileReceiverCommand);
_sockecClient.ReadStream.ReadTimeout = int.MaxValue;
EnableTcpService();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
#endregion
#region TcpService
private void EnableTcpService()
{
_cancelationTcpService = new CancellationTokenSource();
new Task(StartService, _cancelationTcpService.Token, TaskCreationOptions.LongRunning).Start();
}
private void StartService()
{
while (!_cancelationTcpService.Token.IsCancellationRequested)
{
var ev = EvenNotProtocol.DeserializeEvent(_reader);
if (ev == null) continue;
_writter.WriteString(EvenNotProtocol.MobileOkCommand);
EventReceived?.Invoke(this, new CentralTcpEventArgs(ev));
}
}
}
public class EvenNotProtocol
{
public static Event DeserializeEvent(ExtendedBinaryReader reader)
{
try
{
reader.SkipBytes(1);
.....
}
catch (IOException e)
{
return null;
}
}
}
请发布您正在使用的相关代码完成,我用我的代码更新了。有人能帮我吗?