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