In most cases encode/decode will help ensure that the message payload is delivered and parsed by the receiver correctly. I ran across a case with numpy arrays where I needed to use serialization to send the data. In rare cases, you may want to consider using cPickle (its a built-in Python library for serialization)... you can import cPickle, and use cPickle.dumps to load your payload in the sender code; then use cPickle.loads on the receiver. Just remember that the max message size on DXL is currently 1 MB.
sample snippets:
- from dxlclient.client import DxlClient
- from dxlclient.client_config import DxlClientConfig
- from dxlclient.message import Event
- import cPickle, numpy
- CONFIG_FILE = "/usr/local/etc/opendxl/dxlclient.config"
- config = DxlClientConfig.create_dxl_config_from_file(CONFIG_FILE)
- EVENT_TOPIC = "/open/event/numpy"
- numpypayload= "<numpy array here>"
- with DxlClient(config) as client:
- client.connect()
- event = Event(EVENT_TOPIC)
- event.payload = cPickle.dumps(numpypayload)
- logger.info("Publishing XML to %s", EVENT_TOPIC)
- client.send_event(event)
Display More
- import dxlclient, cPickle
- CONFIG_FILE = "/usr/local/etc/opendxl/dxlclient.config"
- config = DxlClientConfig.create_dxl_config_from_file(CONFIG_FILE)
- EVENT_TOPIC = "/open/event/numpy"
- ...
- with dxlclient.client.DxlClient(config) as client:
- print "Connecting to DXL..."
- client.connect()
- print "Connected to DXL & waiting for events!"
- class numpy(dxlclient.callbacks.EventCallback):
- def on_event(self, event):
- numpypayload= cPickle.loads(event.payload)
- print "Received: %r" %numpypayload
- client.add_event_callback(EVENT_TOPIC, numpy())
- while True:
- time.sleep(60)
Display More