2
我試圖基於基於HTML5的通知系統,基於數據庫中某個表上發生的事情。我專門爲此通知功能製作了表格。這是如何工作的:通過cURL發送HTML5服務器事件
- 服務器發送活動頁面(
sse.html
)將參考source.php
其通知內容。 action.php
包含通過將數據插入到表中來觸發內容通知更改的操作。source.php
將定期檢查表中是否有新的數據。如果有的話,它應該通過一個通知。
我的問題是否有可能如果這樣做而不檢查表?
在執行action.php
中的操作時,我正在考慮使用cURL將內容直接發送到source.php
。這可以做到嗎?
我看過cURL與HTTP頭Content-Type
設置爲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;
}
值得一提的是,有更好的方法來做到這一點,比如使用消息隊列的pub/sub。你應該看看這個視頻:http://vimeo.com/20605470 – igorw
謝謝igorw ...我會檢查出來 – mosh