In the samples for the OpenDXL Python Client, and all of the solutions I've seen, the payload for any type of DXL message (event/request/response) has .encode() called on it before sending. And anyone that receives the message calls .decode() on it immediately. See below for an example of what I'm referring to:
Python: event_example.py
- # ... skipping some code
- # Create and add event listener
- class MyEventCallback(EventCallback):
- def on_event(self, event):
- with event_count_condition:
- # Print the payload for the received event
- print "Received event: " + event.payload.decode()
- # Increment the count
- event_count[0] += 1
- # Notify that the count was increment
- event_count_condition.notify_all()
- # ... skipping some code
- # Loop and send the events
- for event_id in range(TOTAL_EVENTS):
- # Create the event
- event = Event(EVENT_TOPIC)
- # Set the payload
- event.payload = str(event_id).encode()
- # Send the event
- client.send_event(event)
- # ... skipping some code
Why is it even necessary to encode and decode the payload if we know the data type ("string", for example)? Is there a reason to do this every time?