【问题标题】:how to send json header in websocket tornado python?如何在 websocket 龙卷风 python 中发送 json 标头?
【发布时间】:2016-11-28 18:19:01
【问题描述】:
 def on_message(self, message):

     for client in ChatWebSocket.clients:
         print(client)
         t=json.loads(message)
         client.write_message(json.dumps('Content-type:application/json\n')) 
         client.write_message(json.dumps({'a':t['b']}))
         print(t['b'])

问题是客户端像普通字符串一样接受它而不是标题 请帮忙

【问题讨论】:

    标签: python-2.7 websocket tornado


    【解决方案1】:

    来自Tornado's documentation

    WebSocketHandler.write_message(message, binary=False)

    将给定的消息发送到此 Web Socket 的客户端。

    消息可能是要么字符串或字典(将被编码为json)。如果 binary 参数为 false,则消息将作为 utf8 发送;在二进制模式下,任何字节串都是允许的。

    所以你不需要倾倒任何东西。只需按原样发送字典,Tornado 无论如何都会将其编码为 JSON: false,消息将以 utf8 格式发送;在二进制模式下,任何字节串都是允许的。

    所以你不需要倾倒任何东西。只需按原样发送字典,Tornado 就会

    def on_message(self, message):
      for client in ChatWebSocket.clients:
        print(client)
        t = json.loads(message)
        client.write_message({'a': t['b']})
        print(t['b'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多