当前位置: 动力学知识库 > 问答 > 编程问答 >

Javascript EventSource simultaneous with xmlhttprequest interacting with a PHP server

问题描述:

I'm trying to use EventSource in javascript. But as soon as I use them, i'm having some trouble. First, when reloading the page, the EventSource can't reconnect. Second, using it prevents XmlHttpRequests to work properly. In the below code, if I comment the event_source related code, the xml request works fine. If I create the event source and add a listener, the xml request never succeed.

envent_source = new EventSource('event-source.php');

envent_source.addEventListener('update', function(event) {

console.log("update event");

}, false);

var request = new XMLHttpRequest();

request.onreadystatechange = function() {

if (this.readyState == this.DONE) {

if (this.status == 200)

console.log("received");

}

}

request.open("GET", "document.xml");

request.send();

Did I missed something ? Do EventSource needs to be used with caution ?

网友答案:

As pointed out by vinyar, the problem isn't client side but server side. In my case, the server uses php sessions which ensure mutual exclusive executions of requests from the same client. Thus the GET requests are put in a waiting state until the event source script is terminated.

More information there : Simultaneous Requests to PHP Script

分享给朋友:
您可能感兴趣的文章:
随机阅读: