PHP事件源继续执行 [英] PHP Event Source keeps executing

查看:115
本文介绍了PHP事件源继续执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用JavaScript EventSource对象在HTML5中使用push。我对PHP中的一个工作解决方案非常满意:

  $ time = 0; 
while(true){
if(connection_status()!= CONNECTION_NORMAL){
mysql_close()
break;
}
$ result = mysql_query(SELECT`id` FROM`table` WHERE UNIX_TIMESTAMP(`lastUpdate`)>'。$ time。');
while($ row = mysql_fetch_array($ result)){
echodata:。$ row [id]。PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
$ time = time();
sleep(1);
}

但是突然间我的WebApp无法再被MySQL错误访问太多连接。



事实证明,在关闭JavaScript中的事件源后,MySQL连接不会关闭:

  window.onload = function(){
sse = new EventSource(push.php);
sse.onmessage = function(event){
console.log(event.data.split(:)[1]);


window.onbeforeunload = function(){
sse.close();
}

所以我猜PHP脚本不会停止执行。在客户端连接断开之前,是否有任何方法可以调用函数(如 die(); )?为什么我的脚本在EventSource中调用 .close(); 之后没有终止?!



感谢您的帮助! -

解决方案

首先:当您将代码封装在一个巨大的中时(true) loop,你的脚本永远不会终止。当你的脚本用完了代码执行时,与数据库的连接会被关闭,但是因为你写了一个死锁,所以不会发生......永远。 >它不是 EventSource 问题,它只是做它应该做的事情。诚实,真实,可悲的是你的错。



事情是:用户连接到您的网站, EventSource 对象被实例化。建立到服务器的连接并请求返回值 push.php 的请求。您的服务器按要求运行,并运行该脚本,这只是一个僵局。没有错误,只要php.ini允许它运行,它就可以继续运行。 .close()方法不会取消输出流(或者应该),但是脚本执行无限循环或休眠时非常繁忙。假设脚本被停止是因为客户端断开连接就像假设任何客户端可能会干扰服务器的功能。安全方面,这将是最糟糕的情况。现在,回答您的实际问题:导致问题的原因以及解决方法?
答案: HEADERS



这个小小的PHP示例:脚本不保持活动状态服务器端(通过无限循环),但通过设置正确的标题。



正如旁注:Please,ditch mysql _ * 尽快使用(最后),使用 PDO mysqli _ * istead。老实说,这并不难。

I started using push in HTML5 using the JavaScript EventSource object. I was totally happy with a working solution in PHP:

$time = 0;
while(true) {
    if(connection_status() != CONNECTION_NORMAL) {
        mysql_close()
        break;
    }
    $result = mysql_query("SELECT `id` FROM `table` WHERE UNIX_TIMESTAMP(`lastUpdate`) > '".$time."'");
    while($row = mysql_fetch_array($result)) {
        echo "data:".$row["id"].PHP_EOL;
        echo PHP_EOL;
        ob_flush();
        flush();
    }
    $time = time();
    sleep(1);
}

But suddenly my WebApp wasn't reachable anymore with an MySQL error "too many connections".

It turned out that the MySQL connection doesn't close after closing the event source in JavaScript:

window.onload = function() {
    sse = new EventSource("push.php");
    sse.onmessage = function(event) {
        console.log(event.data.split(":")[1]);
    }
}
window.onbeforeunload = function() {
    sse.close();
}

So I guess that the PHP script does not stop to execute. Is there any way to call in function (like die();) before the clients connection disconnects? Why doesn't my script terminate after calling .close(); on the EventSource?!

Thanks for help! —

解决方案

First off: when you wrap your code in a huge while(true) loop, your script will never terminate. The connection with the DB would have been closed when your script "ran out of code to execute", but since you've written a deadlock, that's not going to happen... ever.
It's not an EventSource issue, that merely does what it's supposed to do. It honestly, truly, and sadly is your fault.

The thing is: a user connects to your site, and the EventSource object is instantiated. A connection to the server is established and a request for the return value of push.php is requested. Your server does as requested, and runs the script, that -again- is nothing but a deadlock. There are no errors, so it can just keep on running, for as long as the php.ini allows it to run. The .close() method does cancel the stream of output (or rather it should), but your script is so busy either performing its infinite loop, or sleeping. Assuming the script to be stopped because a client disconnects is like assuming that any client could interfere with what the server does. Security-wise this would be the worst that could happen. Now, to answer your actual question: What causes the issue, how to fix it?
The answer: HEADERS

Look at this little PHP example: the script isn't kept alive server-side (by infinite loops), but by setting the correct headers.

Just as a side-note: Please, ditch mysql_* ASAP, it's being deprecated (finally), use PDO or mysqli_* istead. It's not that hard, honestly.

这篇关于PHP事件源继续执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆