我有一个脚本触发SSE事件,用于从online.php获取json编码数据。 在谷歌搜索,我找到了通过引入换行符来发送带有sse的JSON数据的方法。
我正在寻找的是当使用PHP的json_encode()函数创建JSON数组时如何通过SSE发送JSON。
我已经编写了以下代码行,但是有人可以帮助我在哪里添加"数据:\ n \ n" SSE需要什么?
<script>
if(typeof(EventSource)!=="undefined")
{
var source=new EventSource("online.php");
source.onmessage=function(event)
{
var data=JSON.parse(event.data);
$("#new_message").html("Inbox"+data['total']);
};
}
else
{
$("#new_message").html("HTML5 not supported");
}
</script>
online.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$data["total"]="hello";
echo json_encode($data);
ob_flush();
flush();
?>
答案 0 :(得分:11)
您需要以EventStream格式发送它,在这种情况下,它只是在data:
echo 'data: ' . json_encode($data) . "\n\n";
答案 1 :(得分:5)
您可以像Ryan所说的那样编码$data
数组:
echo 'data: ' . json_encode($data) . "\n\n";
然后,客户端event.data
将被视为一个字符串,然后您可以使用查询jQuery.parseJSON()
轻松解析为json。所以你的客户端代码看起来像这样:
// Check if the browser supports SSE
if (typeof (EventSource) !== "undefined") {
var source = new EventSource("script.php");
// Handle evetns
source.onmessage = function(event) {
// parse the data that has an object as a string
var msg = $.parseJSON(event.data);
// Do awesome code with the values inside msg
};
} else {
alert("Sorry, your browser doesn't support this awesome feature!");
}
答案 2 :(得分:1)
您的脚本只会显示输出一次,因为它需要某种循环才能继续运行(当然有条件或者您将运行数百万个实例!!)。
我已经切断了我今天早些时候写的一个实现,它演示了这一点并添加了一些额外的javascript / jquery来帮助更好地管理流。下面也适用于像Xampp这样的单线程PHP安装(用于本地开发) 关于Xampp的注意事项:由于PHP脚本处于循环中并且不立即终止,因此将停止运行新的php或agax脚本。如果您还在使用ajax调用前调中的PHP调用stream_close()和成功回调中的stream_open()。
以下内容尚未经过测试,但主要是从工作代码中抓取,所以应该没问题。
<?
//stream.php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
stream();
function stream(){
$data = array();
//collect data from database or wherever to stream to browser
//example data
$data[0]["name"] = 'Bob';
$data[0]["total"] = rand(0,100);
$data[0]["name"] = 'Jane';
$data[0]["total"] = rand(0,100);
//maybe there is no new data so just send one new line
//this is required to check if the connection is still alive
if(!empty($data)){
echo "\n";
}else{ //Otherwise json encode the data for output
echo 'data: '.json_encode($data)."\n\n";
}
flush(); //Flush the result to the browser
sleep(1); //Wait a second (or what ever you like)
//If the browser is still connected
if(!connection_aborted() && connection_status()==0){
stream(); //recurse the function
}
}
?>
<script>
var webstream = false;
function stream_open(){
stream_close(); //Close the stream it (in case we got here weirdly)
if(!!window.EventSource){ //Test compatibility
webstream = new EventSource('./stream.php');
console.log("Stream Opened"); //Log event for testing
webstream.addEventListener('message', function(e){
var data = JSON.parse(e.data); //Parse the json into an object
process_stream(data);
},false);
//Cleanup after navigating away (optional)
$(window).bind('beforeunload', function(){
webstream.onclose = function(){}; //delete onclose (optional)
webstream.close(); //Close the stream
});
}
}
function stream_close(){
if(typeof(webstream)=="object"){
webstream.close();
webstream = false;
console.log("Stream Closed"); //Log event for testing
}
}
function process_stream(data){
//do something with the new data from the stream, e.g. log in console
console.log(data);
}
//Optional:
//Toggle stream on blur/focus
//Good if the user opens multiple windows or Xampp?
$(window).on("blur focus", function(e) {
//get the last blur/focus event type
var prevType = $(this).data("prevType") || null;
if (prevType != e.type){
console.log(e.type); //Log event for testing (focus/blur)
switch (e.type){
case "blur":
stream_close(); //Close stream on blur
break;
case "focus":
stream_open(); //Open stream on focus
break;
}
}
//Store the last event type to data
$(this).data("prevType", e.type);
});
// Optional:
// Using idletimer plugin to close the stream in times of inactivity
// https://github.com/thorst/jquery-idletimer/blob/master/src/idle-timer.js
$(document).on("idle.idleTimer", function (){
stream_close();
});
$(document).on("active.idleTimer", function (){
stream_open();
});
$(document).idleTimer({timeout:5000}); //5 second idle timer
</script>