jasonrbriggs / stomp.py

“stomp.py” is a Python client library for accessing messaging servers (such as ActiveMQ or RabbitMQ) using the STOMP protocol (versions 1.0, 1.1 and 1.2). It can also be run as a standalone, command-line client for testing.
Apache License 2.0
491 stars 167 forks source link

WSConnection does not handle binary messages #417

Open christopherjameshoward opened 1 year ago

christopherjameshoward commented 1 year ago

Using stomp.py-8.1.0

The server I am connecting to is RabbitMQ with the rabbitmq_web_stomp plugin.

The web stomp plugin is configured to send binary messages, as documented here: https://www.rabbitmq.com/web-stomp.html#content-encoding:

web_stomp.ws_frame = binary

When I attempt to open a connection to RabbitMQ using the stomp.WSConnection class, I receive an exception:

Traceback (most recent call last):
  File "c:\git\dai\venv\lib\site-packages\stomp\transport.py", line 376, in __read
    c = self.receive()
  File "c:\git\dai\venv\lib\site-packages\stomp\adapter\ws.py", line 252, in receive
    return self.socket.recv().encode()
AttributeError: 'bytes' object has no attribute 'encode'

which leads to a ConnectFailedException:

File "c:\git\dai\venv\lib\site-packages\stomp\connect.py", line 199, in connect
    Protocol12.connect(self, *args, **kwargs)
  File "c:\git\dai\venv\lib\site-packages\stomp\protocol.py", line 529, in connect
    self.transport.wait_for_connection()
  File "c:\git\dai\venv\lib\site-packages\stomp\transport.py", line 318, in wait_for_connection
    raise exception.ConnectFailedException()
stomp.exception.ConnectFailedException

The problem seems to be that the WSTransport.receive method assumes that all websocket messages are strings, and tries to convert them back into bytes: https://github.com/jasonrbriggs/stomp.py/blob/dev/stomp/adapter/ws.py#L215

I think it would be possible to fix this issue by calling the recv_data method, rather than recv:

opcode, data = self.socket.recv_data()
return data

If you agree, I would be happy to submit a PR.

mfmarche commented 1 year ago

hi, that seems like a reasonable solution. There would need to be a configuration at the WSConnection constructor, default as tring, since by default RabbitMQ is configured as UTF-8

By default, the Web STOMP plugin will expect to handle messages encoded as UTF-8
christopherjameshoward commented 1 year ago

Here is the PR: https://github.com/jasonrbriggs/stomp.py/pull/419