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

Javascript 使用Autobahn Python和Google Chrome的websocket通信延迟,javascript,python,html,websocket,autobahn,Javascript,Python,Html,Websocket,Autobahn,以下是我的工作内容: webserver.py: import sys from twisted.internet import reactor from twisted.python import log from autobahn.websocket import WebSocketServerFactory, \ WebSocketServerProtocol, \

以下是我的工作内容:

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分钟也不会产生任何结果,我必须在看到任何消息之前手动刷新页面。知道是什么原因导致了这种情况吗?

您尝试过WS-echo示例吗?是的,它具有完全相同的效果。请尝试禁用任何桌面防火墙等。尝试在与浏览器相同的计算机上运行服务器。试试Chrome和Firefox。上面的例子确实有效。。问题必须源于您的网络/防火墙环境。。