我有一个像这样运行的TCPServer:
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>hey there</p>
<a href="/auth/google">Sign In With Google</a>
</header>
</div>
首先,取消令牌不起作用。我想这是另一个问题。
我的问题是检测退出的套接字。如果另一端的套接字关闭得很好,我会收到零字节(缓存在await Task.Run(() =>
{
try
{
client = tcpClient;
bool Stop = false;
byte[] response = new byte[1024];
while (!ServerController.Token.IsCancellationRequested && !Stop)
{
Array.Clear(response, 0, response.Length);
var amount = tcpClient.Client.Receive(response);
string data = Encoding.UTF8.GetString(response);
if (amount == 0)
{
Stop = true;
ServerController.Token.ThrowIfCancellationRequested();
}
else
ProcesNewMessage(data);
}
if (ServerController.Token.IsCancellationRequested)
{
tcpClient.Close();
ServerController.Token.ThrowIfCancellationRequested();
}
}
catch (Exception exc)
{
tcpClient.Close();
Debug(exc.Message);
ServerController.Token.ThrowIfCancellationRequested();
}
}, ServerController.Token).ContinueWith(
(result) =>
{
clientDisconnected?.Invoke(this, EventArgs.Empty);
});
}
变量中),但真正麻烦的情况是客户端意外断开连接(关闭电源,关闭终端等)等)如何检测到它们?
我的客户是像Arduino一样的芯片,因此它们很容易意外死亡。我怎么注意到它?
PS:我想避免心跳,因为我的设备使用电池供电,而通过WiFi发送是我的主要能量消耗