[英]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.