繁体   English   中英

Web套接字未连接到龙卷风服务器

[英]Web Socket not connecting to tornado server

我正在使用python龙卷风框架为正在编写的游戏编写小型Web服务器。 但是,当我尝试创建websocket连接时,get请求工作正常,在浏览器中出现此错误:

在此处输入图片说明

这是我的JavaScript代码:

    var ws = new WebSocket("ws://localhost:8888/ws");
    ws.onopen = function() {
            ws.send("ping");
    };

这是python服务器的代码:

class StateQueryHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        state.players = state.players + 1
        self.write(state.players)
        print("socket opened")

application = tornado.web.Application([
    (r"/ws", StateQueryHandler),#websocket endpoint
    (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": "../client"})
])
server = tornado.httpserver.HTTPServer(application)
server.listen(8888)
tornado.ioloop.PeriodicCallback(state.update, 250).start()
tornado.ioloop.IOLoop.instance().start()

谁能告诉我怎么了? 为了使tcp连接保持活动状态,我是否需要在服务器端执行其他任何操作?

尝试这个:

class StateQueryHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        state.players = state.players + 1
        self.write_message(state.players)
        print("socket opened")

您需要调用方法write_message,而不是write。

请查看文档以获取更多信息: http : //www.tornadoweb.org/en/branch2.4/websocket.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM