dtvpl739577 2017-08-14 11:57
浏览 11
已采纳

服务器端事件和Ajax请求

Using PHP and JavaScript I am trying to add a button to my website that will 'resume' a live feed of data. I can successfully 'stop' the feed, I'm just struggling to start it again.

When I stop the feed, I am saving the the lastEventId coming from the server. When I click the start button I re-use this value and send an AJAX request to the server. This works and I am able to retrieve the lastEventId.

I need some help starting the feed again from where it was stopped.

My JS code;

<script type="text/javascript">
       $("document").ready(function(){
           var lastSerial;
           var source = new EventSource("data.php");

           source.onmessage = function(event) {
               lastSerial = event.lastEventId;
               document.getElementById("result").innerHTML += "New transaction: " + event.data + "<br>";
               console.log(event.lastEventId); // returns the `lastEventId`
           };
           $("#start").click(function(){
               $.ajax({
                   type: "POST",
                   url: 'data.php',
                   data: {lastSerial: lastSerial},
                   success: function(data){
                       // need to start the feed again here from where it left off, based on the returned `lastSerial` value
                       console.log(lastSerial) // returns an INT as expected
                   }
               });
           });
           $("#stop").click(function(){
               source.close();
           });
       });//end dom ready
</script>

<div id="result"><!--live feed here--></div>
<button id="stop"> stop</button>
<button id="start"> start</button>

My data.php (simplified);

if(isset($_POST['lastSerial'])) {
    // SELECT TimeStamp, SerialNo ... WHERE SerialNo >= 'lastSerial' ...
    // fetch results
    // echo "data: " .$row["SerialNo"]. "

";
}

So as things stand, I can successfully stop the feed. When I click start the lastSerial is logged to the console.

Any advice is appreciated.

  • 写回答

1条回答 默认 最新

  • duanqiang3925 2017-08-14 12:02
    关注

    Instead of doing source.close() use a flag to determine whether the feed has been stopped.

    var is_stopped = false;
    
    [...]
    
    $("#stop").click(function(){
        is_stopped = true;
    });
    

    Then,

    source.onmessage = function(event) {
        /** If it is NOT stopped. **/
        if (!is_stopped) {
            lastSerial = event.lastEventId;
            document.getElementById("result").innerHTML += "New transaction: " + event.data + "<br>";
            console.log(event.lastEventId); // returns the `lastEventId`
        }
    };
    

    Or,

    source.onmessage = function(event) {
        /** If it IS stopped. **/
        if (is_stopped)
            return false;
    
        lastSerial = event.lastEventId;
        document.getElementById("result").innerHTML += "New transaction: " + event.data + "<br>";
        console.log(event.lastEventId); // returns the `lastEventId`
    };
    

    That way you aren't actually killing the event, so when you want to restart the feed you just set is_stopped to false and everything resumes as before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法