62 lines
No EOL
1.3 KiB
Python
62 lines
No EOL
1.3 KiB
Python
#!/usr/bin/python3
|
|
import pika
|
|
import json
|
|
import os
|
|
|
|
rabbitmq_host = os.environ.get('RABBITMQ_HOST')
|
|
|
|
|
|
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host))
|
|
channel = connection.channel()
|
|
channel.queue_declare(queue='signal-receive')
|
|
|
|
def demo_echo(text, sender, destination):
|
|
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host))
|
|
channel = connection.channel()
|
|
channel.queue_declare(queue='signal-send')
|
|
|
|
|
|
message = [
|
|
text,
|
|
destination,
|
|
sender
|
|
]
|
|
|
|
message_json = json.dumps(message)
|
|
|
|
channel.basic_publish(exchange='',
|
|
routing_key='signal-send',
|
|
body=message_json)
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
|
|
def callback(ch, method, properties, body):
|
|
body = json.loads(body)
|
|
text = body[0]
|
|
timestamp = body[1]
|
|
sender = body[2]
|
|
reciever = body[3]
|
|
demo_echo(text, sender, reciever)
|
|
|
|
# This could be used to distribute messages to multiple servers. Just acknowledge message only if you should. (eq. based on rec number)
|
|
channel.basic_ack(delivery_tag=method.delivery_tag)
|
|
|
|
|
|
|
|
channel.basic_consume('signal-receive', callback, auto_ack=False)
|
|
|
|
|
|
try:
|
|
print("starting consuming")
|
|
channel.start_consuming()
|
|
except KeyboardInterrupt:
|
|
channel.stop_consuming()
|
|
connection.close()
|
|
|
|
|
|
|
|
|
|
# message_json = json.dumps(message) |