最近我们需要检测客户端何时从服务器突然中止/断开连接.突然间,我的意思是不一定通过浏览器的STOP按钮,实际上,客户端是通过POST(使用soapUI)发送的SOAP消息,因此,断开连接可能就像在收到客户端之前停止(Ctrl + C)客户端一样简单一个回应.我们花了几天时间试图找到解决方案,直到找到方法.因此,这可能是一个隐含响应的问题,但目标是提供可以帮助许多其他人同样需要的信息.
以下是我们使用服务器检测客户端断开连接的基本代码:
<?PHP
ignore_user_abort(true); // Continue running the script even when the client aborts/disconnects
sleep(5); // Sleep 5 seconds so we can stop the client before it recieves a reponse from the server
echo "RESPONSE sent to the client"; // Response to the Request
ob_flush(); // Clean output buffer
flush(); // Clean PHP's output buffer
usleep(500000); // Sleep half a second in order to detect if Apache's server …Run Code Online (Sandbox Code Playgroud) php ×1