Bytes and string both get encoded as strings
See original GitHub issueHello community,
I have a problem when sending a byte array with the use of the msgpack serializer. When receiving the data, it is unpack as a string.
When using the json serializer, the data is correctly encoded, as referenced in this document, and decoded as bytes.
I also made Wireshark captures which show that the following byte array b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10'
, is encoded as follows ["\u0000b'AQIDBAUGBwgJEA==' "]
, when using the json serializer. This encoding corresponds well to that described in the standard cited in the previous document.
With the msgpack serializer, the byte array is not encoded correctly and send derecly as follow 0a 01 02 03 04 05 06 07 08 09 10
here is my current configuration:
- Python3.8
- autobahn 20.4.3
- msgpack 1.0.0
Below an example allowing to highlight the problem:
Backend.py
import asyncio
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.component import Component, run
class Backend(ApplicationSession):
def __init__(self, config=None):
config.realm = 'TESTS'
super().__init__(config)
async def onJoin(self, details):
print('Register test_echo_payload')
await self.register(self.test_echo_payload, 'test.echo.payload')
async def onDisconnect(self):
loop = asyncio.get_event_loop()
loop.stop()
async def test_echo_payload(self, value: bytes) -> bytes:
if not isinstance(value, bytes):
print('Value is not an instance of bytes but is a %s' % (type(value)))
return value
if __name__ == '__main__':
backend = Component(
session_factory = Backend,
transports = [
{
'type': 'websocket',
'serializers': ['msgpack'],
'url': 'ws://localhost:8902/ws',
'max_retries': 3
}
]
)
run([backend])
Client.py
import asyncio
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.component import Component, run
class Client(ApplicationSession):
def __init__(self, config=None):
config.realm = 'TESTS'
super().__init__(config)
async def onJoin(self, details):
print('Call test_echo_payload')
loop = asyncio.get_running_loop()
loop.create_task(self.test_echo_payload())
async def onDisconnect(self):
loop = asyncio.get_event_loop()
loop.stop()
async def test_echo_payload(self):
message_bytes = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10'
ret = await self.call('test.echo.payload', message_bytes)
if __name__ == '__main__':
client = Component(
session_factory = Client,
transports = [
{
'type': 'websocket',
'serializers': ['msgpack'],
'url': 'ws://localhost:8902/ws',
'max_retries': 3
}
]
)
run([client])
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Hello Tobias,
In the end everything works very well … I had to mix in my many tests … (Python, C++, Json, MsgPack, …)
Thank you again for your help. You can close this ticket.
Thank you Tobias for your analysis and your reactivity!
In order to complete the COTS used, I tested with the V20.4.2 Crossbar WAMP router. Below, its configuration:
Let’s get rid of C++ and go back to my original problem. The problem being that when we send a message of type “bytes” while serializing with msgpack, we receive a message of type “str” …
I just did some additional tests which make me think that the problem comes from the router, I explain. Here are the steps of my test:
The Client component correctly sends the byte table type message encoded in msgpack as follows:
0x9530ce0001e24080b1746573742e6563686f2e7061796c6f616491c40a01020304050607080910
But on the Backend component, we receive the message of the following form:It seems that the Crossbar router returns the message in Json format and not Msgpack…