10

我已经使用龙卷风建立了一个非常基本的 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>

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

已连接
发送:您好!
响应:Hello World
已断开连接

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

WebSocket 已经处于 CLOSING 或 CLOSED 状态。

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

4

1 回答 1

11

德普。onMessage 函数正在关闭它:

function onMessage(evt) { 
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
    websocket.close();
}
于 2013-11-13T06:01:32.287 回答