python tornado.websocket 服务端 向客户端发送消息 write_message 报错 求大神呀呀呀

```class htmlHandel(RequestHandler):

    def get(self, url):
        if ("%s.html" % (url,)) in os.listdir("./html"):
            self.render(os.path.abspath('./html/%s.html' % (url,)))

        else:
            self.render(os.path.abspath('./html/404.html'))


class MyWebSocketHandler(WebSocketHandler):
    # 保存连接的用户,用于后续推送消息
    user = []

    # 判断源origin,对于符合条件的请求源允许链接
    def check_origin(self, origin):
        return True

    @classmethod
    def open(self):
        print("有人连接进来")

        # 打开连接时将用户保存到connect_users中
        self.user.append(self)
        for user in self.user:
            user.write_message("我是服务端")

    @classmethod
    def on_message(self, message):
        print('收到的信息为:' + message)

    @classmethod
    def on_close(self):
        print("WebSocket closed")
        # 关闭连接时将用户从connect_users中移除
        self.user.remove(self)

    def check_origin(self, origin):
        # 此处用于跨域访问
        return True

    @classmethod
    def send_demand_updates(cls, message):
        # 使用@classmethod可以使类方法在调用的时候不用进行实例化
        # 给所有用户推送消息(此处可以根据需要,修改为给指定用户进行推送消息)
        for user in cls.user:
            user.write_message(message)


def start_server(port):
    # 创建启动项 启动项负责配置映射规则
    app = tornado.web.Application([
        (r'/api', MyWebSocketHandler),
        # (r'/css/(.*.css)', tornado.web.StaticFileHandler, {"path": os.path.abspath("./static/css")}),  # 映射本地文件
        (r'/js/(.*.*)', tornado.web.StaticFileHandler, {"path": os.path.abspath("./static/js")}),
        # (r'/img/(.*)', tornado.web.StaticFileHandler, {"path": os.path.abspath("./static/img")}),
        (r'/(.*)', htmlHandel),

    ], debug=True)
    host = "localhost"
    print("服务已启动 http://%s:%s" % (host, port))

    server = HTTPServer(app, max_buffer_size=10240000, max_body_size=10240000)
    # 绑定端口
    server.bind(port)
    server.start()
    # 启动服务
    tornado.ioloop.IOLoop.instance().start()


if __name__ == '__main__':
    start_server(8000)

![报错信息](https://img-ask.csdn.net/upload/202006/03/1591178481_825692.png)

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问