我试图使用一个简单的长轮询系统(是的,我做了而不是想要使用任何现成的yep,我想从中学习)。我使用的是节点服务器,可以轻松地将数据管道/写入到客户端,而无需调用result.end();
。我该怎么做这个客户端?对于使用ie<=9的用户来说,我只希望这是一个简单的、不太好的退路,因为更好的浏览器可以快速、容易地使用websocket。
长问题短:如何在没有jQuery或其他框架的普通JS中进行长轮询?(或者有没有比长时间投票更好的方式)。
发布于 2014-02-14 22:33:19
下面是你要找的东西吗?
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';//or 'text', 'json', ect. there are other types.
xhr.timeout = 60000;//milliseconds until timeout fires. (1 minute)
xhr.onload = function(e2){
var blob = xhr.response;
//handle response data
}
xhr.ontimeout = function(){
//if you get this you probably should try to make the connection again.
//the browser should've killed the connection.
}
xhr.open('GET', "/path/to/URL.cmd?param1=val1¶m2=val2", true);
xhr.send();
我认为timeout
属性是使长轮询正常工作的关键,有关更多信息,请参见等级库,在同一文档中有关于responseType的更多信息。如果未指定超时,则默认值为零。
https://stackoverflow.com/questions/21789425
复制相似问题