我正在尝试创建一个基于 HTML5 的通知系统,基于我的数据库中的表上发生的事情。我专门为此通知功能制作了表格。这是它的工作原理:
- 服务器发送事件页面 (
sse.html
) 将参考source.php
其通知内容。 action.php
包含通过向表中插入数据来触发内容更改通知的操作。source.php
将定期检查表中的任何新数据。如果有的话,它应该通过一个通知。
我的问题是是否可以在不检查表格的情况下执行此操作?
我正在考虑使用 cURL 将内容直接发送到执行source.php
操作时action.php
。这可以做到吗?
Content-Type
我已经看到将 HTTP 标头设置为的 cURL 使用text/event-stream
,但我不确定如何使用它。
sse.html
<!DOCTYPE html>
<html>
<body>
<script>
var source = new EventSource('source.php');
source.onmessage = function(event){
var text = event.data;
window.webkitNotifications.createNotification('', 'Alert', text).show();
}
</script>
</body>
</html>
source.php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
mysql_connect("localhost", "user", "pass");
mysql_select_db("eventstream");
$q = mysql_query("select textnotif from notification where read='0'");
$r = mysql_fetch_array($q);
$notif = $r[textnotif];
if($notif != ""){
echo "data: ".$notif.PHP_EOL;
}