简体   繁体   English

当调用on_message时,龙卷风websocket收到多条消息

[英]tornado websocket get multi message when on_message called

我使用龙卷风websocket发送/接收消息,客户端发送json消息,以及服务器recv消息和json解析,但是为什么服务器会得到mutil json消息,例如{"a":"v"}{"a":"c"} ,如何处理此消息

Maybe you should delimit the messages you send so it is easy to split them up - in this case you could add a \\n, obviously the delimiter mustn't happen within the message. 也许您应该分隔发送的消息,以便轻松拆分它们-在这种情况下,您可以添加\\ n,显然分隔符一定不能出现在消息中。 Another way would be to prefix each message with its length in also a clearly-delimited way, then the receiver reads the length then that number of bytes and parses it. 另一种方法是,也以清晰界定的方式在每个消息的长度前加上前缀,然后接收方读取该长度,然后读取该字节数并对其进行解析。

I write a fucntion to parse this message 我写了一个功能来解析此消息

def parse_multi_msg(msg):
  is_in_quotation = False
  aObjs = []
  sTemp = ""
  for c in msg:
    sTemp += c;

    if c == '"':
      if not is_in_quotation:
        is_in_quotation = True
      else:
        is_in_quotation= False


    if c == "}" and not is_in_quotation:
      aObjs.append(json.loads(sTemp));
      sTemp = "";

  return aObjs

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

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