为了账号安全,请及时绑定邮箱和手机立即绑定

可以在脚本执行时使用 Ajax post 调用的 php 脚本发回 SSE 事件吗

可以在脚本执行时使用 Ajax post 调用的 php 脚本发回 SSE 事件吗

PHP
慕仙森 2022-10-28 14:50:49
我的服务器上有一个执行多项任务(设置文件夹、复制文件等)的 php 脚本,我用 ajax 调用该脚本。IE注册.js:const evtSource = new EventSource("/createuser.php", { withCredentials: true } );evtSource.onmessage = function(event) {  console.log(event.data);}$.ajax({    type:     'POST',    url:      '/createuser.php',    dataType: 'json',    data: {        'username': "test user",         'usertype' : "author"    },    success: function(data){        console.log("user created");    }});现在在 createuser.php 上,我尝试向我的网页发送一条消息:创建用户.php:<?phpfunction SendProgressMessage($op){    header('Content-Type: text/event-stream');    header('Cache-Control: no-cache');    switch($op){        case "userCreated":            echo "User Created";        break;        case "foldersCreated":            echo "Folder structure created";        break;        case "TemplatesCopied":            echo "Templates Copied";        break;    }    flush();}?>我可以将 evtSource 设置为相同的脚本和 ajax 调用,还是为脚本创建 2 个会话?
查看完整描述

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

你不能这样设置,AJAX 请求会在服务器端产生一个新的 PHP 进程,它不知道第一个进程。

长时间工作的脚本应该将状态存储到数据库中,从那里可以通过第二个(,第三个,...)请求独立查询。


查看完整回答
反对 回复 2022-10-28
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信