我正在使用 HTML5 Server-Sent Events .
实际上我需要显示通知(新记录输入和未读记录),即在数据库(php/mysql)中插入任何新记录时。
因此,出于测试目的,我只是尝试使用总行数。但是我在本地主机中收到此错误消息:
Firefox can't establish a connection to the server at http://localhost/project/folder/servevent/demo_sse.php.
行是:
var source = new EventSource("demo_sse.php");
我试过这个:
index.php
<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
<div id="result"></div>
demo_sse.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$db = mysql_connect("localhost", "root", ""); // your host, user, password
if(!$db) { echo mysql_error(); }
$select_db = mysql_select_db("testdatase"); // database name
if(!$select_db) { echo mysql_error(); }
$time = " SELECT count( id ) AS ct FROM `product` ";
$result = mysql_query($time);
$resa = mysql_fetch_assoc($result);
echo $resa['ct'];
flush();
?>
请让我知道出了什么问题。
我知道通知我们可以在一定的间隔时间使用 Ajax,但我不想要这样的东西。因为我有 N 条记录,这可能会减慢我的资源。
最佳答案
关于javascript - 服务器发送事件 : Not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25155818/