2016-02-11 63 views
0

我正在使用appengine通道API(延遲任務),但它似乎並沒有工作。調試通道API Appengine

下面是一個要點我的服務器代碼:

Class Handler(webapp2.RequestHandler): 
    def get(self): 
    path = jinja_environment.get_template('templates/new_console.html') 
    token = channel.create_channel('some_key') 
    # Deferring the task. 
    deferred.defer(_task, token) 
    args = {} 
    args['token'] = token 
    self.response.out.write(path.render(args)) 



def _task(token): 
    FeedbackThreadModel(id='id').put() 
    time.sleep(60) 
    channel.send_message(token, 'done') 

這是我的JavaScript客戶端

<html> 
    <head> 
    <script type="text/javascript" src="/_ah/channel/jsapi"></script> 
    <script> 
     channel = new goog.appengine.Channel('{{ token }}'); 
     socket = channel.open(); 
     socket.onmessage = onMessage; 
     onMessage = function() { 
      var xhr = new XMLHttpRequest(); 
      xhr.open('GET', '/secondpage'); 
      xhr.send(); 
     }; 
     </script> 
    </head> 

我希望GET請求被髮起的網址:任務後「視爲/ secondpage」是完整的,但是這沒有發生。我究竟做錯了什麼 ?

+0

你的任務實際需要多長時間?你有沒有試過用倒計時來延遲任務,因爲它可能發生在瀏覽器創建頻道之前。同時檢查瀏覽器和服務器上的日誌。實際發生了什麼? –

+0

@PaulCollingwood謝謝,試過但沒有幫助。 – Shan

回答

0

這現在似乎工作。顯然,套接字請求的JavaScript應該在HTML體而不是頭。工作JavaScript看起來像這樣:

<html> 
    <body> 
    <script type="text/javascript" src="/_ah/channel/jsapi"></script> 
    <script> 
     channel = new goog.appengine.Channel('{{ token }}'); 
     socket = channel.open(); 
     socket.onmessage = function() { 
      var xhr = new XMLHttpRequest(); 
      xhr.open('GET', '/secondpage'); 
      xhr.send(); 
     }; 
     </script> 
    </body>