Server Sent Events Stop Start with new parameter

只谈情不闲聊 提交于 2019-12-01 05:20:53

问题


please help.

I'm using Server Sent Events to dynamically update a website based on data stored in a database.

I now wish to pass a new parameter ('abc.php/?lastID=xxx') back to the PHP script based on data received in the previous message.

I understand i can use event.close to stop the current 'stream' but im struggling to work out how to use the same 'EventListener' for each new EventSource where the URL is dynamically created

Can somebody point me in the right direction please?

Thanks,


回答1:


Ok spent some time pretty much getting better grip on Javascript.

var source = new EventSource("xyz.php?last=0");
source.addEventListener('message', onMessageHandler,false);

function onMessageHandler(event) {
   console.log(event.data);//just for debug purposes

   event.target.close(); //close current stream.
   var last = "1234"; // loaded from event.data in my solution.
   var sourceNew = new EventSource("xyz.php?last=" + last); // start another 
   sourceNew.addEventListener('message', onMessageHandler, false); //event

}

and i seem to have solved the problem i had.

Ta



来源:https://stackoverflow.com/questions/13685965/server-sent-events-stop-start-with-new-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!