7

我有一个脚本可以触发 SSE 事件以从 online.php 获取 json 编码数据。在谷歌搜索中,我找到了通过引入换行符来使用 sse 发送 JSON 数据的方法。

我正在寻找的是如何在使用 PHP 的 json_encode() 函数创建 JSON 数组时通过 SSE 发送 JSON。

我已经编写了以下代码行,但是有人可以帮我在哪里添加 SSE 所需的“数据:\n\n”吗?

<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>

在线.php

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$data["total"]="hello";
echo json_encode($data);
ob_flush();
flush(); 
?>
4

3 回答 3

12

您需要以 EventStream 格式发送它,在这种情况下,它只是在它前面加上data:

echo 'data: ' . json_encode($data) . "\n\n";
于 2013-09-15T13:15:37.577 回答
5

$data您可以像 Ryan 所说的那样对数组进行编码:

echo 'data: ' . json_encode($data) . "\n\n";

然后,客户端event.data将被视为一个字符串,然后您可以使用查询轻松地将其解析为 json jQuery.parseJSON()。所以你的客户端代码看起来像这样:

// 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!");
        }

来源:http ://api.jquery.com/jquery.parsejson/

于 2014-09-04T22:08:36.277 回答
2

您的脚本只会显示一次输出,因为它需要某种循环才能继续运行(当然有条件,否则您将运行数百万个实例!!)。

我已经切碎了我今天早些时候编写的一个实现,它演示了这一点,还添加了一些额外的 javascript/jquery 来帮助更好地管理流。以下内容也适用于单线程 PHP 安装,例如 Xampp(用于本地开发) Xampp 上的注意事项: 由于 PHP 脚本处于循环中并且不会立即终止,它将停止运行新的 php 或 agax 脚本。如果您还使用 ajax 来调用 PHP,则在 beforesend 中调用 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>
于 2014-09-26T03:53:11.180 回答