为什么我的WebSocket会自动关闭?

时间:2013-11-13 05:10:14

标签: javascript python websocket tornado

我使用龙卷风设置了一个非常基本的websocket服务器:

import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.httpserver

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hello World")

    def on_message(self, message):
        print 'message received %s' % message

    def on_close(self):
      print 'connection closed'


application = tornado.web.Application([
  (r'/ws', WSHandler),
])


if __name__ == "__main__":
  http_server = tornado.httpserver.HTTPServer(application)
  http_server.listen(8000)
  tornado.ioloop.IOLoop.instance().start()

和一个非常基本的客户端,应该能够通过点击按钮持续ping服务器:

<!DOCTYPE html>  
<meta charset="utf-8" />  
<title>WebSocket Test</title>  
<script language="javascript" type="text/javascript">

    var wsUri = "ws://localhost:8000/ws";
    var websocket = new WebSocket(wsUri);
    var output;
    function init() { 
        output = document.getElementById("outputDiv");
        if (!'WebSocket' in window){
            writeToScreen('<span style="color: red;">ERROR: Update your browser to one that supports websockets. A list can be found <a href="http://caniuse.com/websockets">here</a></span>');
        } else {
            testWebSocket();
        }
    }
    function onOpen(evt) { 
        writeToScreen("CONNECTED");
        doSend("Hi there!");
    }
    function onClose(evt) { 
        writeToScreen("DISCONNECTED");
    }
    function onMessage(evt) { 
        writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
        websocket.close();
    }
    function onError(evt) { 
        writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
    }
    function doSend(message) { 
        writeToScreen("SENT: " + message);
        websocket.send(message);
    }
    function writeToScreen(message) { 
        var pre = document.createElement("p");
        pre.style.wordWrap = "break-word";
        pre.innerHTML = message;
        output.appendChild(pre);
    }
    function testWebSocket() {
        websocket.onopen = function(evt) { onOpen(evt) };
        websocket.onclose = function(evt) { onClose(evt) };
        websocket.onmessage = function(evt) { onMessage(evt) };
        websocket.onerror = function(evt) { onError(evt) };
        document.getElementById("send").onclick = function () {
            doSend(document.getElementById("inputTxt").value);
            console.log("click registered.");
        };

    }
    window.addEventListener("load", init, false);
</script>  
</div>
<h2>WebSocket Test</h2> 
<div id="inputDiv">
    <input id="inputTxt" type="text"/>
    <button id="send" onclick=>send</button>
</div>
<div id="outputDiv">
</div>
</html>

当我加载页面时,我得到:

  

CONNECTED
  SENT:你好!
  回应:Hello World
  断开连接

如果我点击该按钮,则会收到错误消息:

  

WebSocket已处于CLOSING或CLOSED状态。

但是,如果我在命令行中重新实例化websocket对象(通过chrome开发人员工具),按钮就可以工作,并且连接没有关闭。我觉得这一定是我不理解的javascript的一些特质,因为如果对象仍然在范围内,我不明白为什么它会自动关闭连接。

1 个答案:

答案 0 :(得分:9)

DERP。 onMessage函数正在关闭它:

function onMessage(evt) { 
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
    websocket.close();
}