0

まあ、私のの使用のeventsourceは私のブラウザをクラッシュさせています。eventsourceが私のブラウザをクラッシュさせています

ステータスの表を表示する単純なページと、更新のためにサーバーから送信されたイベントをリッスンするjavascriptがあります。私はjquery.eventsourceを使用してリスニングを行い、jQueryバージョン1.6.2を使用しています。私はFirefox 10をブラウザとして使用しています。サーバー上で私はpython 2.7.2とcherrypyを使用しています。3.2.2

ステータスページを実行したままにしておいても、それを更新しないとうまくいくようです。ページを数回(最後のカウントで15回)リフレッシュしたり、ページを数回移動したりすると、ブラウザがクラッシュすることがあります。

このクラッシュの原因は何ですか?

私はGoogle Chrome 17.0.963.78 mを使用してこれを試しましたが、これは問題ありません。 Chromeはクラッシュしません。

これは私のjavascript(status.js)です:

jQuery(document).ready(function() 
      { 
      jQuery.eventsource(
       { 
       label: 'status-source', 
       url: 'statusUpdates', 
       dataType: 'json', 
       open: function(data){}, 
       message: function(data) 
       { 
        cell = jQuery('#'+data.htmlID); 
        cell.text(data.value); 
       } 
       } 
      ); 
      } 
     ); 

これはHTMLです:

<html> 
    <head> 
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> 
    <title>Event source test page</title> 
    <script src="js/jquery.js" type="text/javascript"></script> 
    <script src="js/jquery.eventsource.js" type="text/javascript"></script> 
    <script src="js/status.js" type="text/javascript"></script> 
    </head> 
    <body> 
    <table> 
     <tr> 
    <th>name</th><th>value</th> 
     </tr> 
     <tr> 
    <td>Heads</td><td id="headval">4</td> 
     </tr> 
     <tr> 
    <td>Hands</td><td id="handval">16</td> 
     </tr> 
     <tr> 
    <td>Feet</td><td id="feetval">24</td> 
     </tr> 
     <tr> 
    <td>Eyes</td><td id="eyeval">18</td> 
     </tr> 
     <tr> 
    <td>Fingers</td><td id="fingerval">1</td> 
     </tr> 
    </table> 
    </body> 
</html> 

これは、私の知る限りことができますようにCherryPyにサーバー

import cherrypy 
import os 
import Queue 
import threading 
import random 
import json 

class Server(object): 

    def __init__(self): 
     self.isUpdating = True 
     self.statusUpdateList = Queue.Queue() 
     self.populateQueue() 
     threading.Timer(1, self.queuePopulationRepetition).start() 

    def stop(self): 
     self.isUpdating = False 

    def queuePopulationRepetition(self): 
     self.populateQueue() 
     if self.isUpdating: 
      threading.Timer(1, self.queuePopulationRepetition).start() 

    def populateQueue(self): 
     self.statusUpdateList.put(json.dumps({ 'htmlID':'headval', 'value':random.randint(0,50) })) 
     self.statusUpdateList.put(json.dumps({ 'htmlID':'handval', 'value':random.randint(0,50) })) 
     self.statusUpdateList.put(json.dumps({ 'htmlID':'feetval', 'value':random.randint(0,50) })) 
     self.statusUpdateList.put(json.dumps({ 'htmlID':'eyeval', 'value':random.randint(0,50) })) 
     self.statusUpdateList.put(json.dumps({ 'htmlID':'fingerval', 'value':random.randint(0,50) })) 

    @cherrypy.expose 
    def index(self): 
     f = open('index.html', 'r') 
     indexText = '\n'.join(f.readlines()) 
     f.close() 
     return indexText 

    @cherrypy.expose 
    def statusUpdates(self, _=None): 
     cherrypy.response.headers["Content-Type"] = "text/event-stream" 
     self.isViewingStatus = True 
     if _: 
      data = 'retry: 400\n' 
      while not self.statusUpdateList.empty(): 
       update = self.statusUpdateList.get(False) 
       data += 'data: ' + update + '\n\n' 
      return data 
     else: 
      def content(): 
       update = self.statusUpdateList.get(True, 400) 
       while update is not None: 
        data = 'retry: 400\ndata: ' + update + '\n\n' 
        update = self.statusUpdateList.get(True, 400) 
        yield data 
      return content() 
    statusUpdates._cp_config = {'response.stream': True, 'tools.encode.encoding':'utf-8'}     


if __name__ == "__main__": 

    current_dir = os.path.dirname(os.path.abspath(__file__)) 

    cherrypy.config.update({'server.socket_host': '0.0.0.0', 
          'server.socket_port': 8081, 
          }) 

    conf = { 
      "/css" : { 
       "tools.staticdir.on": True, 
       "tools.staticdir.dir": os.path.join(current_dir, "css"), 
       }, 
      "/js" : { 
       "tools.staticdir.on": True, 
       "tools.staticdir.dir": os.path.join(current_dir, "js"), 
       }, 
      "/images" : { 
       "tools.staticdir.on": True, 
       "tools.staticdir.dir": os.path.join(current_dir, "images"), 
       }, 
     } 

    cherrypy.quickstart(Server(), "", config=conf) 

答えて

2

ですそれは、ブラウザをクラッシュさせているjQueryプラグインです。私は平文EventSourceオブジェクトを使用するためにJavaScriptを書き直しました。これは問題を解決したようです。

jQuery(document).ready(function() 
         { 
          var source = new EventSource('statusUpdates'); 
          source.addEventListener('message', 
                function(receivedObject) 
                { 
                 var data = jQuery.parseJSON(receivedObject.data); 
                 var cell = jQuery('#'+data.htmlID); 
                 cell.text(data.value); 
                 var status = cell.siblings(':last'); 
                 status.removeClass(); 
                 status.addClass(data.status); 
                }, false); 

         } 
        ); 
関連する問題