因此,我在让服务器端事件与PHP一起工作时遇到了问题.我在网上try 了使用无限While循环的各种不同的教程,但我所有的try 都在几分钟后以500错误告终,而不是发送数据.
对于我的应用程序,我需要脚本在无限循环中运行,因为我正在通过TCP套接字从硬件设备获取更新,而要使设备向我发送更新,我必须保持套接字连接打开.
My setup is as follows:
PHP Version 7.3.33
Windows Server 2022 with IIS 10 (Also tried on Windows Server 2016 with IIS 10)
output_buffering and zlib.output_compression are both set to "Off"
responseBufferLimit is set to 0 in the FastCGI Handler settings for PHP.
我目前使用的基本测试代码如下:
Sse.php
<?PHP
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
while(true) {
$now = date('r');
echo "data: The time is : {$now}\n\n";
@ob_flush();
flush();
sleep(2);
}
?>
Test.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<script>
var source = new EventSource("Sse.php");
source.onopen = function(e) {
console.log("The connection to your server has been opened");
}
source.onmessage = function(e) {
console.log ("message: " + e.data);
}
source.onerror = function(e) {
console.log("The server connection has been closed due to some errors");
}
</script>
</body>
</html>
这(理论上)应该每隔2秒向客户端发送一个当前时间的数据字符串,但实际上是500个错误.我必须补充的是,只有在实现While循环时才会出现这种情况.如果我删除了While循环,那么它会让客户端每隔3秒重新连接一次(这对我的应用程序不起作用).
The PHP logs only show the following errors which don't appear to be linked to my issue, unless anyone can suggest otherwise:
[17-Nov-2022 21:26:28 UTC] PHP Warning: Module 'mysqli' already loaded in Unknown on line 0
[17-Nov-2022 21:26:28 UTC] PHP Warning: Module 'mbstring' already loaded in Unknown on line 0
[17-Nov-2022 21:26:28 UTC] PHP Deprecated: Directive 'track_errors' is deprecated in Unknown on line 0
几天来,我一直在寻找一种解决方案,但到目前为止还没有奏效.有没有人对需要更改IIS/PHP设置以使其正常工作的潜在设置有什么建议?
一些教程建议进行以下配置更改: Php.ini>;Output_Buffering和zlib.OutputCompression值设置为"OFF" 在PHP的FastCGI处理程序设置中,ResponseBufferLimit设置为0.
但这两点都没有对 playbook 产生任何影响.