Server-sent events and php - what triggers events on the server?

前端 未结 5 1944
执念已碎
执念已碎 2020-12-07 11:49

All,

HTML5 Rocks has a nice beginner tutorial on Server-sent Events (SSE):

http://www.html5rocks.com/en/tutorials/eventsource/basics/

But, I don\'t u

5条回答
  •  感情败类
    2020-12-07 12:16

    I have notice that the sse techink sends every couple of delay data to the client (somtething like reversing the pooling data techink from client page e.x. Ajax pooling data.) so to overcome this problem i made this at a sseServer.php page :

    
    

    and the sse.php is :

    
    

    Notice that at the sseSerer.php i start a session and using a session variable! to overcome the problem.

    Also i call the sseServer.php via Ajax (posting and set value to variable message) every time that i want to "update" message.

    Now at the jQuery (javascript) i do something like that : 1st) i declare a global variable var timeStamp=0; 2nd) i use the next algorithm :

    if(typeof(EventSource)!=="undefined"){
            var source=new EventSource("sseServer.php");
            source.onmessage=function(event)
            if ((timeStamp!=event.lastEventId) && (timeStamp!=0)){
                    /* this is initialization */
                    timeStamp=event.lastEventId;
                    $.notify("Please refresh "+event.data, "info");
            } else {
                    if (timeStamp==0){
                             timeStamp=event.lastEventId;
                    }
            } /* fi */
    
    } else {
            document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
    } /* fi */
    

    At the line of : $.notify("Please refresh "+event.data, "info"); is there that you can handle the message.

    For my case i used to send an jQuery notify.

    You may use POSIX PIPES or a DB Table instead to pass the "message" via POST since the sseServer.php does something like an "infinite loop".

    My problem at the time is that the above code DOES NOT SENDS THE "message" to all clients but only to the pair (client that called the sseServer.php works as individual to every pair) so i'll change the technik and to a DB update from the page that i want to trigger the "message" and then the sseServer.php instead to get the message via POST it will get it from DB table.

    I hope that i have help!

提交回复
热议问题