Commits

Anonymous committed 61ff5b3

Added some example scripts to demonstrate more real-world usage.

Comments (0)

Files changed (3)

examples/publisher.py

+import time
+import logging
+import pickle
+from datetime import datetime
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+
+from stompclient import PublishClient
+
+client = PublishClient('127.0.0.1', 61613)
+client.connect()
+
+try:
+    payload = {'key': 'value', 'counter': 0, 'list': ['a', 'b', 'c'], 'date': datetime.now()}
+    while True:
+        logger.debug("Sending message: {0}".format(payload))
+        client.send('/queue/example', pickle.dumps(payload, protocol=pickle.HIGHEST_PROTOCOL))
+        time.sleep(1.0)
+        payload['counter'] += 1
+        payload['date'] = datetime.now()
+finally:
+    client.disconnect()

examples/subscriber.py

+"""
+An example demonstrating running stompclient in subscribe-only mode.
+
+stompclient can be used without a separate listener thread if you only want to 
+subscribe to incoming messages (and don't need to get fancy in how you handle
+the received frames).
+"""
+import logging
+import pickle
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+from stompclient import PublishSubscribeClient
+
+def frame_received(frame):
+    # Do something with the frame!
+    payload = pickle.loads(frame.body)
+    logger.info("Received data: {0!r}".format(payload))
+
+client = PublishSubscribeClient('127.0.0.1', 61613)
+client.connect()
+client.subscribe("/queue/example", frame_received)
+client.listen_forever()

examples/subscriber_thread.py

+"""
+An example desmontrating a publish-subscribe usage of stompclient.
+
+In this example it is necessary to start a thread listener loop to receive the
+frames from the server.  When the listener loop is running other "response" 
+frames, such as the CONNECTED frame, will also be returned by the 
+PublishSubscribeClient.
+"""
+import threading
+import logging
+import time
+import pickle
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+from stompclient import PublishSubscribeClient
+
+def frame_received(frame):
+    # Do something with the frame!
+    payload = pickle.loads(frame.body)
+    logger.info("Received data: {0!r}".format(payload))
+
+client = PublishSubscribeClient('127.0.0.1', 61613)
+listener = threading.Thread(target=client.listen_forever, name='Frame-Receiver')
+listener.start()
+
+# For our example, we want to wait until the server is actually listening
+client.listening_event.wait()
+
+try:
+    result = client.connect()
+    logger.info("Got session response from connect: {0}".format(result.session))
+    client.subscribe("/queue/example", frame_received)
+    
+    while True:
+        time.sleep(1.0)
+            
+finally:
+    client.shutdown_event.set()
+    listener.join()