不完整的二进制消息在Python STOMP客户端中接收

huangapple go评论118阅读模式
英文:

Incomplete binary messages received in Python STOMP client

问题

我正在尝试在两个Python STOMP客户端之间交换二进制消息。

消息发送和接收没有问题。但是,接收方总是丢失几个字节(似乎是随机的)。

C++客户端可以完整接收相同的消息。当发送ASCII文本时也没有问题。

我错过了什么?

  • Python版本: 3.10
  • Stomp版本: 7.0.0
  • ActiveMQ版本: 5.16.3

发布者:

  1. import random
  2. import string
  3. import time
  4. import sys
  5. import stomp
  6. import os
  7. hosts = [('localhost', 61613)]
  8. conn = stomp.Connection(host_and_ports=hosts)
  9. conn.connect('admin', 'admin', wait=True)
  10. while 1:
  11. b = bytes(os.urandom(100))
  12. #b = ''.join(random.choices(string.ascii_lowercase, k=1500))
  13. print(len(b))
  14. conn.send(content_lengh=len(b), body=b, destination='/queue/test')
  15. time.sleep(2)
  16. conn.disconnect()

订阅者:

  1. import time
  2. import sys
  3. import stomp
  4. class MyListener(stomp.ConnectionListener):
  5. def on_error(self, frame):
  6. print('received an error "%s"' % frame.body)
  7. def on_message(self, frame):
  8. print(type(frame.body))
  9. print('received message len = "%s"' % len(frame.body))
  10. conn = stomp.Connection()
  11. conn.set_listener('', MyListener())
  12. conn.connect('admin', 'password', wait=True)
  13. conn.subscribe(destination='/queue/test', id=1, ack='auto')
  14. print("Waiting for messages...")
  15. while 1:
  16. time.sleep(10)
  17. conn.disconnect()
英文:

I am trying to exchange binary messages between two Python STOMP clients.

Message sending and receiving have no issues. However, there are always a few bytes (seems random) missing on the receiver.

The same messages are received in whole by C++ clients. No issue either when ASCII text are sent.

What am I missing?

  • Python: 3.10
  • Stomp: 7.0.0
  • ActiveMQ: 5.16.3

Publisher:

  1. import random
  2. import string
  3. import time
  4. import sys
  5. import stomp
  6. import os
  7. hosts = [('localhost', 61613)]
  8. conn = stomp.Connection(host_and_ports=hosts)
  9. conn.connect('admin', 'admin', wait=True)
  10. while 1:
  11. b = bytes(os.urandom(100))
  12. #b = ''.join(random.choices(string.ascii_lowercase, k=1500))
  13. print(len(b))
  14. conn.send(content_lengh=len(b), body=b, destination='/queue/test')
  15. time.sleep(2)
  16. conn.disconnect()

Subscriber:

  1. import time
  2. import sys
  3. import stomp
  4. class MyListener(stomp.ConnectionListener):
  5. def on_error(self, frame):
  6. print('received an error "%s"' % frame.body)
  7. def on_message(self, frame):
  8. print(type(frame.body))
  9. print('received message len = "%s"' % len(frame.body))
  10. conn = stomp.Connection()
  11. conn.set_listener('', MyListener())
  12. conn.connect('admin', 'password', wait=True)
  13. conn.subscribe(destination='/queue/test', id=1, ack='auto')
  14. print("Waiting for messages...")
  15. while 1:
  16. time.sleep(10)
  17. conn.disconnect()

答案1

得分: 0

我通过跟踪 STOMP 协议自己找到了解决方法。订阅者需要禁用自动解码,如下所示。否则,帧的内容将被解码为 UTF-8。

  1. conn = stomp.Connection(auto_decode=False)
英文:

I figured it out myself by tracing STOMP. The subscriber needs to disable auto decode as shown below. Otherwise, the frame body will be decoded as UTF-8.

  1. conn = stomp.Connection(auto_decode=False)

huangapple
  • 本文由 发表于 2023年2月24日 03:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定