3

웹 서버를 작성하는 데 Twisted를 사용하고 있습니다. 이 서버가 수행하는 작업 중 하나가 오래 걸립니다 (~ 5 분). 이 작업이 완료되었음을 클라이언트에 효율적으로 알리고 싶습니다.Twisted : 서버 측 프로세스가 완료되면 클라이언트에 알립니다.

나는 혜성/롱 폴링을 사용하는 방법을 살펴 보았지만 필자의 경험으로는 브라우저가받은 데이터를 렌더링 할 수 없다.

이 메커니즘을 프로토 타입, 나는 다음 썼다 :

clock.py

from twisted.internet import reactor, task 
from twisted.web.static import File 
from twisted.web.server import Site 
from twisted.web import server 
from twisted.web.resource import Resource 
import time 

class Clock(Resource): 
    def __init__(self): 
     self.presence=[] 
     loopingCall = task.LoopingCall(self.__print_time) 
     loopingCall.start(1, False) 
     Resource.__init__(self) 

    def render_GET(self, request): 
     print "request from",request.getClientIP() 
     request.write(time.ctime()) 
     self.presence.append(request) 
     return server.NOT_DONE_YET 

    def __print_time(self): 
     print "tick" 
     for p in self.presence: 
      print "pushing data to",p.getClientIP() 
      p.write(time.ctime()) 

root = Resource() 
clock = ClockPage() 
index = File("index.html") 
root.putChild("index.html",index) 
root.putChild("clock",clock) 
factory = Site(root) 
reactor.listenTCP(8080, factory) 
reactor.run() 

index.html을 나는에 해왔 무엇

<html> 
<head> 
</head> 
<body> 
<div id="data">Hello</div> 
<script type="text/javascript"> 
var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function(){ 
    if(xhttp.readyState == 4 && xhttp.status == 200){ 
    alert(xhttp.responseText); 
    document.getElementById("data").innerHTML=xhttp.responseText; 
    } 
}; 
xhttp.open("GET","clock",true); 
xhttp.send(null); 
</script> 
</body> 
</html> 

서버 측은 매시간 request.write을 호출합니다.

클라이언트 측에서는 .readyState == 4.status == 200 일 때마다 적절한 리소스에 XMLHTTPRequest를 열고 responseText을 div에 직접 덤프합니다.

문제는 다음과 같습니다. div가 결코 덮어 쓰이지 않으며 경고가 호출되지 않습니다.

나는 multipart/x-mixed-replace을 사용하는 것에 대해 계속 읽었지만 어떻게 사용하는지 잘 모르겠습니다. 이런 종류의 것을 꼬여서 구현하는 자습서 나 문서에 대한 지침은 크게 감사하겠습니다.

답변

0

루프에 p.finish()을 추가하면 요청이 실제로 완료됩니다. 현재 구현은 영원히 중단됩니다.

0

"HTTP 스트리밍"은 어떻게 사용합니까? 서버에서 "수신 대기"브라우저로 로그를 "스트리밍"하는데 성공적으로 사용했습니다. 다음은 꼬인 ​​부분과 약간의 js를 사용하는 간단한 구현입니다. http://codelab.ferrarihaines.com/archives/161

+0

여기에는 저자 정보가 없습니다. –