Skip to content

Instantly share code, notes, and snippets.

@Morabaraba
Last active August 7, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Morabaraba/b14d92a9ca1a4e5b817cf0f9a8b8cd3c to your computer and use it in GitHub Desktop.
Save Morabaraba/b14d92a9ca1a4e5b817cf0f9a8b8cd3c to your computer and use it in GitHub Desktop.
Python bert.decode not working between Javascript and Python.

So I'm using Bert in js with MQTT pushing it over ws to rabbitmq where a pika consumer reads the body and tries to decode the js BERT encoded body; and it fails.

    def on_message(self, unused_channel, basic_deliver, properties, body):
        print("raw body:")
        print(body)
        print("oct body:")
        for c in body: print(c)
        berty = bert.decode(body)

Output

raw body:
b'\xc2\x83h\x06k\x00\x03movk\x00\x07Anon839n\x06\x00f\xc3\xa0\xc3\x94`V\x01b\x00\x00\x03\x1bb\x00\x00\x01Ph\x02d\x00\x04bertd\x00\x05false'
oct body:
194
131
104
6
107
0
3
109
111
118
107
0
7
65
110
111
110
56
51
57
110
6
0
102
195
160
195
148
96
86
1
98
0
0
3
27
98
0
0
1
80
104
2
100
0
4
98
101
114
116
100
0
5
102
97
108
115
101
Traceback (most recent call last):
  File "consume.py", line 363, in <module>
    main()
  File "consume.py", line 357, in main
    example.run()
  File "consume.py", line 328, in run
    self._connection.ioloop.start()
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/adapters/select_connection.py", line 354, in start
    self.poll()
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/adapters/select_connection.py", line 602, in poll
    self._process_fd_events(fd_event_map, write_only)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/adapters/select_connection.py", line 443, in _process_fd_events
    handler(fileno, events, write_only=write_only)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/adapters/base_connection.py", line 364, in _handle_events
    self._handle_read()
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/adapters/base_connection.py", line 415, in _handle_read
    self._on_data_available(data)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/connection.py", line 1347, in _on_data_available
    self._process_frame(frame_value)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/connection.py", line 1427, in _process_frame
    self._deliver_frame_to_channel(frame_value)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/connection.py", line 1028, in _deliver_frame_to_channel
    return self._channels[value.channel_number]._handle_content_frame(value)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/channel.py", line 896, in _handle_content_frame
    self._on_deliver(*response)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/pika/channel.py", line 983, in _on_deliver
    header_frame.properties, body)
  File "consume.py", line 276, in on_message
    berty = bert.decode(body)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/bert/codec.py", line 41, in decode
    obj = self.erlang_decoder.decode(bytes, offset)
  File "/home/ubuntu/workspace/pika-consumer/env/lib/python3.4/site-packages/erlastic/codec.py", line 28, in decode
    raise EncodingError("Bad version number. Expected %d found %d" % (FORMAT_VERSION, version))
erlastic.codec.EncodingError: Bad version number. Expected 131 found 194

I generate the body in js with the following code:

chat.Message.prototype.mqtt = function(topic) {
    var t;
    if (this.msg.type == this.types.MESSAGE) {
        t = Bert.tuple(this.msg.type, this.sessionId()  , Date.now(), this.msg.text)
    } else
    if (this.msg.type == this.types.MOVE) {
        t = Bert.tuple(this.msg.type, this.sessionId(), Date.now(), this.msg.x, this.msg.y, this.msg.idle)
    } else
    if (this.msg.type == this.types.CREATE) {
        t = Bert.tuple(this.msg.type, this.sessionId(), Date.now())
    } else
    if (this.msg.type == this.types.ACTION) {
        t = Bert.tuple(this.msg.type, this.sessionId(), Date.now(), this.msg.floor)
    } else 
    if (this.msg.type == this.types.BREAK) {
        t = Bert.tuple(this.msg.type, this.sessionId(), Date.now(), this.msg.x, this.msg.y)
    } else 
    {
        console.error('unknown type for mqtt', msg)
        chat.vm.addMessage(new chat.Message('Sending unknown message type', 'msg', true));
        return;
    }
    var message = new Paho.MQTT.Message(Bert.encode(t));
    message.destinationName = topic || (chat.config.topic + '.' + gm.config.game.level.split('/')[1]);
    console.log(new ArrayBuffer(Bert.encode(t)))
    console.log(message.payloadBytes);
    return message;
}

With output in my browser console:

ArrayBuffer {}
main.js:65 [194, 131, 104, 6, 107, 0, 3, 109, 111, 118, 107, 0, 7, 65, 110, 111, 110, 56, 51, 57, 110, 6, 0, 194, 143, 194, 165, 195, 166, 96, 86, 1, 99, 54, 46, 56, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 55, 49, 52, 48, 51, 54, 101, 43, 48, 49, 0, 0, 0, 0, 0, 97, 194, 176, 104, 2, 100, 0, 4, 98, 101, 114, 116, 100, 0, 5, 102, 97, 108, 115, 101]
main.js:64 ArrayBuffer {}
main.js:65 [194, 131, 104, 6, 107, 0, 3, 109, 111, 118, 107, 0, 7, 65, 110, 111, 110, 56, 51, 57, 110, 6, 0, 194, 159, 194, 165, 195, 166, 96, 86, 1, 97, 72, 97, 194, 176, 104, 2, 100, 0, 4, 98, 101, 114, 116, 100, 0, 5, 102, 97, 108, 115, 101]

Where does the 194 come from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment