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

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

Incomplete binary messages received in Python STOMP client

问题

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

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

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

我错过了什么?

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

发布者:

import random
import string
import time 
import sys
import stomp
import os

hosts = [('localhost', 61613)]
conn = stomp.Connection(host_and_ports=hosts)
conn.connect('admin', 'admin', wait=True)
while 1:
    b = bytes(os.urandom(100))
    #b = ''.join(random.choices(string.ascii_lowercase, k=1500))
    print(len(b))
    conn.send(content_lengh=len(b), body=b, destination='/queue/test')
    time.sleep(2)
    
conn.disconnect()

订阅者:

import time
import sys
import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, frame):
        print('received an error "%s"' % frame.body)

    def on_message(self, frame):
        print(type(frame.body))
        print('received message len = "%s"' % len(frame.body))

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.connect('admin', 'password', wait=True)
conn.subscribe(destination='/queue/test', id=1, ack='auto')

print("Waiting for messages...")
while 1: 
  time.sleep(10)
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:

import random
import string
import time 
import sys
import stomp
import os

hosts = [('localhost', 61613)]
conn = stomp.Connection(host_and_ports=hosts)
conn.connect('admin', 'admin', wait=True)
while 1:
    b = bytes(os.urandom(100))
    #b = ''.join(random.choices(string.ascii_lowercase, k=1500))
    print(len(b))
    conn.send(content_lengh=len(b), body=b, destination='/queue/test')
    time.sleep(2)
    
conn.disconnect()

Subscriber:

import time
import sys
import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, frame):
        print('received an error "%s"' % frame.body)

    def on_message(self, frame):
        print(type(frame.body))
        print('received message len = "%s"' % len(frame.body))

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.connect('admin', 'password', wait=True)
conn.subscribe(destination='/queue/test', id=1, ack='auto')

print("Waiting for messages...")
while 1: 
  time.sleep(10)
conn.disconnect()

答案1

得分: 0

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

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.

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:

确定