无法在rabbitmq通道之后发出。在flask socketio处理程序中启动\u consuming()调用
我正在尝试从flask socketio事件处理程序中侦听rabbitmq队列,以便向web应用程序发送实时通知。到目前为止,我的设置: 服务器 浏览器无法在rabbitmq通道之后发出。在flask socketio处理程序中启动\u consuming()调用,rabbitmq,flask-socketio,Rabbitmq,Flask Socketio,我正在尝试从flask socketio事件处理程序中侦听rabbitmq队列,以便向web应用程序发送实时通知。到目前为止,我的设置: 服务器 浏览器 var socket=io.connect('http://'+document.domain+':8082'); socket.on('connect',函数(resp){ 控制台日志(resp); }); 插座打开(“断开”),功能(resp){ 控制台日志(resp); }); socket.on('error',函数(resp){ 控
var socket=io.connect('http://'+document.domain+':8082');
socket.on('connect',函数(resp){
控制台日志(resp);
});
插座打开(“断开”),功能(resp){
控制台日志(resp);
});
socket.on('error',函数(resp){
控制台日志(resp);
});
socket.on('notification',函数(resp){
控制台日志(resp);
});
如果我注释掉服务器代码底部的“channel.start_consuming()”行并加载浏览器页面,我将成功连接到flask socketio,并在控制台中看到{data:“Connected”}
当我取消注释该行时,我在控制台中看不到{data:“Connected”}。然而,当我向通知队列发送消息时,rabbit_回调函数将启动。我看到我的消息被打印到服务器控制台,但emit调用似乎不起作用。服务器或浏览器上没有错误。任何建议都将不胜感激
谢谢 我在使用eventlet时遇到了同样的问题,我刚刚解决了添加:
import eventlet
eventlet.monkey_patch()
,在我的源代码的开头
无论如何,我的代码有点不同,使用start\u background\u任务方法:
import pika
from threading import Lock
from flask import Flask, render_template, session, request, copy_current_request_context
from flask_socketio import SocketIO, emit, join_room, leave_room, \
close_room, rooms, disconnect
app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
@socketio.on('connect', namespace='/test')
def test_connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=get_messages)
emit('my_response', {'data': 'Connected', 'count': 0})
print('connected')
def get_messages():
channel = connect_rabbitmq()
channel.start_consuming()
def connect_rabbitmq():
cred = pika.credentials.PlainCredentials('username', 'password')
conn_param = pika.ConnectionParameters(host='yourhostname',
credentials=cred)
connection = pika.BlockingConnection(conn_param)
channel = connection.channel()
channel.exchange_declare(exchange='ncs', exchange_type='fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='myexchangename', queue=queue_name)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
return channel
希望这对您有所帮助……您是否在此服务器上使用eventlet或gevent?如果您这样做,您可能需要对std库进行猴子补丁,以使阻塞pika函数成为非阻塞函数。您是否能够解决您的问题?我最终没有使用flask socketio..而是使用模式实现。节点…不是模式
import eventlet
eventlet.monkey_patch()
import pika
from threading import Lock
from flask import Flask, render_template, session, request, copy_current_request_context
from flask_socketio import SocketIO, emit, join_room, leave_room, \
close_room, rooms, disconnect
app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
@socketio.on('connect', namespace='/test')
def test_connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=get_messages)
emit('my_response', {'data': 'Connected', 'count': 0})
print('connected')
def get_messages():
channel = connect_rabbitmq()
channel.start_consuming()
def connect_rabbitmq():
cred = pika.credentials.PlainCredentials('username', 'password')
conn_param = pika.ConnectionParameters(host='yourhostname',
credentials=cred)
connection = pika.BlockingConnection(conn_param)
channel = connection.channel()
channel.exchange_declare(exchange='ncs', exchange_type='fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='myexchangename', queue=queue_name)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
return channel