Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python eventsource正在使我的浏览器崩溃_Python_Firefox_Jquery Plugins_Cherrypy_Server Sent Events - Fatal编程技术网

Python eventsource正在使我的浏览器崩溃

Python eventsource正在使我的浏览器崩溃,python,firefox,jquery-plugins,cherrypy,server-sent-events,Python,Firefox,Jquery Plugins,Cherrypy,Server Sent Events,嗯,我对eventsource的使用正在破坏我的浏览器 我有一个简单的页面,显示状态表,还有一个javascript,用于侦听服务器发送的更新事件。我使用jQuery 1.6.2版来进行监听,我正在运行Firefox10作为我的浏览器。在服务器上,我使用的是python 2.7.2和cherrypy 3.2.2 如果我让状态页面保持运行状态,并且不刷新它,那么它看起来就可以了。如果我多次刷新页面(最后一次刷新15次),或者多次在页面之间来回导航,那么大约一分钟后浏览器就会崩溃 是什么导致了这次坠

嗯,我对eventsource的使用正在破坏我的浏览器

我有一个简单的页面,显示状态表,还有一个javascript,用于侦听服务器发送的更新事件。我使用jQuery 1.6.2版来进行监听,我正在运行Firefox10作为我的浏览器。在服务器上,我使用的是python 2.7.2和cherrypy 3.2.2

如果我让状态页面保持运行状态,并且不刷新它,那么它看起来就可以了。如果我多次刷新页面(最后一次刷新15次),或者多次在页面之间来回导航,那么大约一分钟后浏览器就会崩溃

是什么导致了这次坠机

我已经用谷歌Chrome17.0.963.78M试过了,但这似乎没问题。Chrome不会崩溃

这是我的javascript(status.js):

这是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>

据我所知,是jQuery插件使浏览器崩溃。我重写了javascript,使用了一个普通的
EventSource
对象,这似乎解决了这个问题

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);

                       }
                   );
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)
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);

                       }
                   );