当客户端断开连接时,我在服务器上有以下代码:
更新内部,每帧运行:
foreach (ClientEntity client in connectedClients) {
if (!isClientStillConnected(client.tcpClient)) {
disconnectedClients.Add(client);
client.tcpClient.Close();
Debug.Log (" :: Client " + client.tcpClientName + " has disconnected! ::");
continue;
} else {
Do_The_Stuff_And_Things();
}
}
}
现在,当我在此之后尝试调试时,仍然在Update()内部:
for (int i = 0; i < disconnectedClients.Count; i++) {
Debug.Log(":: Disconnected clients count: " + disconnectedClients.Count.ToString());
Debug.Log(":: Disconnected client at index " + i + ": " + disconnectedClients[i]);
}
这部分是从disconnectedClients列表中删除断开连接的客户端,但当我运行它并退出客户端时,这是Unity在控制台中抛出的内容,只是一个片段:
它继续。所以一个 - for loop 不会停止,这意味着客户端不断被添加到列表中,对吧?现在出了什么问题,如何正确删除(关闭)客户端?
在客户端我运行:
nwReader.Close ();
nwWriter.Close ();
tcpClient.Close ();
socketReady = false;
仅供参考,班级 ClientEntity :
public class ClientEntity {
public TcpClient tcpClient;
public string tcpClientName;
public ClientEntity(TcpClient tcpClientSocket){
tcpClientName = "Guest";
tcpClient = tcpClientSocket;
}
}
答案 0 :(得分:1)
connectedClients
方法在您的情况下正常运行。但是,在断开连接后,您不会从if !isClientStillConnected(client.tcpClient))
List / Array中删除客户端,这会导致//This loop is unchanged
foreach (ClientEntity client in connectedClients)
{
if (!isClientStillConnected(client.tcpClient)) {
disconnectedClients.Add(client);
client.tcpClient.Close();
Debug.Log (" :: Client " + client.tcpClientName + " has disconnected! ::");
continue;
} else {
Do_The_Stuff_And_Things();
}
}
}
//Remove all disconnected clients from the list of connected clients
foreach (var disconnectedClient in disconnectedClients)
{
connectedClients.Remove(disconnectedClient);
}
语句再次评估为true。像这样更改你的代码:
<script src="TESTING123.emcfintech.com/js/superfish.js"></script>