没有ping,Tornado Websockets不会调用on_message

时间:2014-02-05 17:59:55

标签: python websocket tornado

这让我疯狂。我正在使用Tornado 3.2和python 2.7。我在我的测试机器(Windows)和我的Linux机器上都尝试过它。我只是用(我认为会是)一个死的简单聊天室应用来测试水域。这是迄今为止的代码:

class LobbyWS(tornado.websocket.WebSocketHandler):
    clients = []

    def open(self, *args):
        self.stream.set_nodelay(True)
        self.ping('one')
        LobbyWS.clients.append(self)
        print 'opening %s' % self

    def on_pong(self, data):
        print 'got pong', data

    def on_message(self, message):      
        print "Client %s received a message : %s" % (self, message)
        for client in LobbyWS.clients:
            client.write_message(message)

    def on_close(self):
        print "Client %s closed." % self
        LobbyWS.clients.remove(self)

代码似乎工作正常。但是,如果我删除self.ping(),它将停止工作。套接字在浏览器端显示为打开,但是,从不调用服务器上的on_message()函数(Chrome和Firefox中的行为相同)。为什么服务器似乎必须先在套接字上发送数据才能接收它?

1 个答案:

答案 0 :(得分:1)

你的python是正确的,所以我猜错误是在你没有提供的客户端。确保在建立连接之前不要调用send()。

这是一个完整的工作示例:

import tornado.websocket
import tornado.autoreload
import tornado.web

class LobbyWS(tornado.websocket.WebSocketHandler):
    clients = []

    def open(self, *args):
        self.stream.set_nodelay(True)
        LobbyWS.clients.append(self)
        print 'opening %s' % self

    def on_pong(self, data):
        print 'got pong', data

    def on_message(self, message):
        print "Client %s received a message : %s" % (self, message)
        for client in LobbyWS.clients:
            client.write_message(message)

    def on_close(self):
        print "Client %s closed." % self


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("""
<html><head><script>

    console.log('starting ws connection');
  var connection = new WebSocket('ws://127.0.0.1:8888/lobby');

    connection.onopen = function () {
        console.log('open');
        connection.send('Hello Server');
    };

    // Log errors
    connection.onerror = function (error) {
      console.log('WebSocket Error ' + error);
    };

    // Log messages from the server
    connection.onmessage = function (e) {
      console.log('Server: ' + e.data);
    };




</script></head></html>

""")


application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/lobby", LobbyWS),
    ])

if __name__ == "__main__":
    application.listen(8888)
    tornado.autoreload.start()
    tornado.ioloop.IOLoop.instance().start()

希望这有帮助。