首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >事件源正在破坏我的浏览器

事件源正在破坏我的浏览器
EN

Stack Overflow用户
提问于 2012-03-06 10:45:15
回答 1查看 928关注 0票数 0

好吧, my use of eventsource正在破坏我的浏览器。

我有一个简单的页面,它显示状态表,javascript侦听服务器发送的事件以进行更新。我使用jquery.eventsource进行侦听,jQuery版本为1.6.2,并将Firefox10作为浏览器运行。在服务器上,我使用python 2.7.2和cherrypy 3.2.2

如果我保持状态页运行,而不刷新它,那么它似乎是好的。如果我多次刷新页面(最后一次刷新15次),或者多次浏览该页面,那么大约一分钟后浏览器就会崩溃。

是什么导致了这次坠机?

我试过使用谷歌Chrome 170.963.78万,但这似乎不错。铬不会崩溃。

这是我的javascript (status.js):

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
<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服务器

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-16 09:41:34

据我所知,使浏览器崩溃的是jQuery插件。我重写了javascript以使用普通的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);

                       }
                   );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9582083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档