使用Autobahn Python和Google Chrome延迟websocket通信

时间:2012-09-25 00:12:35

标签: javascript python html5 websocket autobahn

以下是我的工作内容:

webserver.py:

import sys

from twisted.internet import reactor
from twisted.python import log

from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS


class EchoServerProtocol(WebSocketServerProtocol):

   def onMessage(self, msg, binary):
      print "sending echo:", msg
      self.sendMessage(msg, binary)


if __name__ == '__main__':

   log.startLogging(sys.stdout)

   factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
   factory.protocol = EchoServerProtocol
   listenWS(factory)

   reactor.run()

background.js:

function updateCookies(info) {
    send();
    console.log(info.cookie.domain);
}

function send() {
    msg = "TEST";
    sock.send(msg);
};

var sock = null;
sock = new WebSocket("ws://localhost:9000");
console.log("Websocket created...");

sock.onopen = function() {
    console.log("connected to server");
    sock.send("CONNECTED TO YOU");
}

sock.onclose = function(e) {
    console.log("connection closed (" + e.code + ")");
}

sock.onmessage = function(e) {
    console.log("message received: " + e.data);
}

chrome.cookies.onChanged.addListener(updateCookies);

现在,在运行webserver.py并运行background.js时,没有任何反应。客户端看到没有回应,服务器不报告任何连接或消息。但是,如果我重新加载background.js,服务器会突然显示“CONNECTED TO YOU”的上一条消息。再次重新加载会产生相同的效果,显示延迟的“已连接到您”消息。我在发送消息后尝试运行sock.close(),但仍然没有产生任何结果。我真的很困惑导致这种随机延迟的原因。让服务器运行10-15分钟也不会产生任何结果,我必须在看到任何消息之前手动刷新页面。知道可能导致这种情况的原因吗?

0 个答案:

没有答案