简体   繁体   English

当用户关闭页面时,如何停止运行服务器发送的事件脚本? PHP

[英]How can i stop a server sent event script from running when the user closes the page? PHP

Im trying to use SSE in PHP backend to send messages to a user. 我试图在PHP后端中使用SSE向用户发送消息。

the functional part of the backend code looks like- 后端代码的功能部分看起来像-

 <?php set_time_limit(0); header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); ignore_user_abort(0); @session_start(); ob_start(); echo "retry: 1000\\n"; ob_flush(); flush(); require_once 'sql_connection_class.php'; require 'chatbox_class.php'; $chatbox=new chatboxclass($_GET['friendid']); function recursivesenddata(){ global $chatbox; $jsonobj=$chatbox->jsonloadunreadmessages(); //append json object if($jsonobj!=-1){ > echo "data:{$jsonobj}\\n\\n"; > ob_flush(); > flush(); > }else{ > //don't send anything > } } while(true){ recursivesenddata(); session_write_close(); sleep(1); } ?> 

it appears that ignore_user_abort(0) doesn't do anything when the user closes the page. 当用户关闭页面时,ignore_user_abort(0)似乎不执行任何操作。

in $chatbox->jsonloadunreadmessages() there is a function that should only be executed when the page is open, it updates things in mySQL database. 在$ chatbox-> jsonloadunreadmessages()中,有一个函数只应在打开页面时执行,它会更新mySQL数据库中的内容。 but this script keeps on running on the server even when the page is closed! 但是即使关闭页面,该脚本仍可以在服务器上继续运行!

is there anyway to check on the server side when the user has closed the page to exit the infinite while loop? 当用户关闭页面以退出无限while循环时,在服务器端是否仍然需要检查?

The PHP script should be killed as soon as the connection socket closes. 连接套接字关闭后,应立即终止PHP脚本。 If the script keeps on running when the user closes the page, there is something very wrong with your Apache/PHP configuration. 如果用户关闭页面时脚本继续运行,则您的Apache / PHP配置存在很大问题。

Socket closing can happen when the client calls EventSource.close() explicitely, the connection is lost due to network problems, the client closes the page or your PHP script terminates. 当客户端明确地调用EventSource.close() ,由于网络问题而导致连接丢失,客户端关闭页面或PHP脚本终止时,可能会发生套接字关闭。

ignore_user_abort(false) does nothing; ignore_user_abort(false)不执行任何操作; that's the default behaviour (the script terminates when the connection is closed). 这是默认行为(关闭连接后脚本终止)。 Passing true as a parameter would have your script survive the connection, but that would not solve your problem. 传递true作为参数会使您的脚本在连接中幸存下来,但这并不能解决您的问题。

Your retry: 1000 serves no purpose since you're never closing the socket server-side. retry: 1000毫无用处,因为您永远不会关闭套接字服务器端。 Your PHP script should be called when the client activates an EventSource object and terminate only at the client's request (or if the network fails), so whatever initial DB tweaking should occur only once per chat connection. 当客户端激活EventSource对象并仅在客户端请求时终止(或如果网络失败),则应调用PHP脚本,因此,任何初始DB调整仅在每个聊天连接中发生一次。 That's assuming the client does not do anything that would close the connection. 假设客户端不执行任何会关闭连接的操作。

Btw this will put a lot of strain to the server: you will have one PHP process per chatting client running for the whole chat duration, and the polling period is about 10 times too small (10 seconds is more than enough). 顺便说一句,这将对服务器造成很大的压力:在整个聊天期间,每个正在运行的聊天客户端将有一个PHP进程,并且轮询时间太短了10倍(10秒就足够了)。 Having the clients poll for new messages every 30 seconds or so would be less wasteful. 让客户每30秒左右轮询一次新消息,这样就不会浪费很多时间。

ignore_user_abort() is ignored on IIS Systems and may be prevented by your configuration. 在IIS系统上将忽略ignore_user_abort(),并且可能被您的配置阻止。

Try to use connection_aborted() in your while loop: 尝试在while循环中使用connection_aborted():

while ( connection_aborted() == 0 ) {
    recursivesenddata();
    session_write_close();
    sleep(1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户关闭浏览器窗口或浏览php中的页面,如何销毁会话? - How can I destroy sessions if user closes the browser window or navigates away from page in php? 如何停止执行我的PHP脚本的某些部分? (以使我的页面加载速度更快) - How can I stop certain part of my php script from executing? (to make my page load faster) 如何在不将用户保留在页面上的情况下从PHP执行PHP脚本? - How can I execute a PHP script from PHP without keeping the user on the page? 如何在HTML5中触发服务器发送的事件...或者:PHP脚本是否可以知道是否已调用另一个脚本? - How to trigger server-sent event in HTML5 … OR : Can PHP script be aware if another script has been called? 服务器发送事件,我可以创建多少个事件源 - Server sent event, how many event source can i create 如何停止PHP脚本? 阻止发送电子邮件? - How to stop a PHP script ? Prevent emails to be sent? 如何停止在PHP中运行的脚本 - how to stop a script running in PHP 从php页面启动在服务器上运行的php脚本(并打开套接字) - launching php script running on server (and opening sockets) from a php page 如何在服务器中运行PHP代码的同时阻止PHP向客户端发送数据? - How do I stop PHP from sending data to client while still running PHP code in server? 当我的服务器运行php7时,如何配置wordpress以从admin运行自动更新 - How can I configure wordpress to run auto updates from admin when my server is running php7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM