我正在尝试从flask-socketio 事件处理程序中收听rabbitmq 队列,以便可以向Web 应用程序发送实时通知。到目前为止我的设置:
服务器
import pika
import sys
from flask import Flask, request
from flask_socketio import SocketIO, emit, disconnect
app = Flask(__name__)
app.config['SECRET_KEY'] = 'not-so-secret'
socketio = SocketIO(app)
def is_authenticated():
return True
def rabbit_callback(ch, method, properties, body):
socketio.emit('connect', {'data': 'yes'})
print "body: ", body
@socketio.on('connect')
def connected():
emit('notification', {'data': 'Connected'})
creds = pika.PlainCredentials(
username="username",
password="password")
params = pika.ConnectionParameters(
host="localhost",
credentials=creds,
virtual_host="/")
connection = pika.BlockingConnection(params)
# This is one channel inside the connection
channel = connection.channel()
# Declare the exchange we're going to use
exchange_name = 'user'
channel.exchange_declare(exchange=exchange_name,
type='topic')
channel.queue_declare(queue='notifications')
channel.queue_bind(exchange='user',
queue='notifications',
routing_key='#')
channel.basic_consume(rabbit_callback,
queue='notifications',
no_ack=True)
channel.start_consuming()
if __name__ == '__main__':
socketio.run(app, port=8082)
浏览器
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://' + document.domain + ':8082');
socket.on('connect', function(resp) {
console.log(resp);
});
socket.on('disconnect', function(resp) {
console.log(resp);
});
socket.on('error', function(resp) {
console.log(resp);
});
socket.on('notification', function(resp) {
console.log(resp);
});
</script>
如果我注释掉服务器代码底部的“channel.start_sumption()”行并加载浏览器页面,我会成功连接到 flask-socketio 并在我的控制台中看到 {data: "Connected"}。
当我取消注释该行时,我在控制台中看不到 {data: "Connected"}。然而,当我向通知队列发送消息时,rabbit_callback 函数会触发。我看到我的消息打印到服务器控制台,但发出调用似乎不起作用。服务器或浏览器中没有错误。非常感谢任何建议。
谢谢!