在这里 https://github.com/Gsantomaggio/rabbitmqexample我写了一个使用龙卷风和RabbitMQ 的完整示例.
您可以从网站上找到所有指令:
无论如何..你需要:
pip install pika
pip install tornado
Run Code Online (Sandbox Code Playgroud)
首先注册你的rabbitmq:
def threaded_rmq():
channel.queue_declare(queue="my_queue")
logging.info('consumer ready, on my_queue')
channel.basic_consume(consumer_callback, queue="my_queue", no_ack=True)
channel.start_consuming()
Run Code Online (Sandbox Code Playgroud)
然后你注册你的网络套接字客户端:
class SocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
logging.info('WebSocket opened')
clients.append(self)
def on_close(self):
logging.info('WebSocket closed')
clients.remove(self)
Run Code Online (Sandbox Code Playgroud)
收到消息后,您可以将其重定向到Web套接字页面.
def consumer_callback(ch, method, properties, body):
logging.info("[x] Received %r" % (body,))
# The messagge is brodcast to the connected clients
for itm in clients:
itm.write_message(body)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2456 次 |
| 最近记录: |