- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在通过 SocketIO 从 RabbitMQ 向用户发送消息时遇到问题。
我有 SocketIO integration 的 Flask 应用程序.当前的用户流看起来像
问题是我无法设置通过 SocketIO 将消息转发到浏览器的 RabbitMQ 监听器。每次我得到不同的错误。主要是连接已关闭,或者我在应用程序上下文之外工作。
我尝试了很多方法,这是我的最后一个。
# callback
def mq_listen(uid):
rabbit = RabbitMQ()
def cb(ch, method, properties, body, mq=rabbit):
to_return = [0] # mutable
message = Message.load(body)
to_return[0] = message.get_message()
emit('report_part', {"data": to_return[0]})
rabbit.listen('results', callback=cb, id=uid)
# this is the page, which user reach
@blueprint.route('/report_result/<uid>', methods=['GET'])
def report_result(uid):
thread = threading.Thread(target=mq_listen, args=(uid,))
thread.start()
return render_template("property/report_result.html", socket_id=uid)
rabbit.listen 方法的抽象如下:
def listen(self, queue_name, callback=None, id=None):
if callback is not None:
callback_function = callback
else:
callback_function = self.__callback
if id is None:
self.channel.queue_declare(queue=queue_name, durable=True)
self.channel.basic_qos(prefetch_count=1)
self.consumer_tag = self.channel.basic_consume(callback_function, queue=queue_name)
self.channel.start_consuming()
else:
self.channel.exchange_declare(exchange=queue_name, type='direct')
result = self.channel.queue_declare(exclusive=True)
exchange_name = result.method.queue
self.channel.queue_bind(exchange=queue_name, queue=exchange_name, routing_key=id)
self.channel.basic_consume(callback_function, queue=exchange_name, no_ack=True)
self.channel.start_consuming()
导致
RuntimeError: working outside of request context
如果有任何提示或使用示例,我将很高兴。
非常感谢
最佳答案
我遇到了类似的问题,归根结底是因为当您发出请求时,flask 会将请求上下文传递给客户端。但解决方案不是添加 app.app_context()。那是 hackey 并且肯定会出现错误,因为您不是 native 发送请求上下文。
我的解决方案是创建一个重定向,以便像这样维护请求上下文:
def sendToRedisFeed(eventPerson, type):
eventPerson['type'] = type
requests.get('http://localhost:5012/zmq-redirect', json=eventPerson)
这是我的重定向函数,所以每当有一个事件我想推送到我的 PubSub 时,它都会通过这个函数,然后推送到那个 localhost 端点。
from flask_sse import sse
app.register_blueprint(sse, url_prefix='/stream')
@app.route('/zmq-redirect', methods=['GET'])
def send_message():
try:
sse.publish(request.get_json(), type='greeting')
return Response('Sent!', mimetype="text/event-stream")
except Exception as e:
print (e)
pass
现在,每当一个事件被推送到我的/zmq-redirect 端点时,它都会被重定向并通过 SSE 发布。
现在最后,只是为了总结一切,客户端:
var source = new EventSource("/stream");
source.addEventListener(
"greeting",
function(event) {
console.log(event)
}
)
关于python - Flask + RabbitMQ + SocketIO - 转发消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29461028/
我正在选择一个 python 库来编写一个嵌入我网站的实时聊天功能页面。 我找到了两个基于 Socket.IO 的库,python-socketio 和 gevent-socketio: 来自 pyt
我正在使用 socketio 和 netty 以及 java,我对它们都是新手。 我的客户端代码如下所示。 webSocket test
我正在尝试在外部值发生变化时发送广播。 Camonitor 在值改变时调用回调,我想通知所有连接的客户端值已经改变,他们需要刷新。 from flask import Flask from epics
我在使用 flasks socketio 时遇到了问题,似乎当我调用 socketio.emit('my event', 'my message') 时它根本不会发出任何事件。 .. 所以这是我的设置
我有一个 socketio 服务器/客户端可以很好地协同工作,但是我想开始为服务器在页面加载或正常运行期间脱机时编写事件。 我在我的头文件中包含了远程 socket.io 代码: /socket.io
我正在使用 flask-socketio 和 python-socketio 开发一个项目。为了确保通信安全,系统需要使用 SSL 进行升级。我正在使用 Mac 和 Python 3.7。 我使用 o
我目前正在尝试了解套接字的工作原理。我正在使用 Flask-socketio 和 python socketio 客户端并运行一个基本示例。这是我到目前为止所做的 应用.py from flask i
我正在尝试使用 Socket.IO 开发一个聊天应用程序。我的服务器端是安装了 express + socket.io 库的 Node.js,我的客户端是 Android + Web。 我尝试在我的客
我正在构建一个 Android 聊天应用程序。我在服务器端使用 nodejs 并尝试实现 android client for socketIO.我收到 SocketIO 异常。代码有什么问题? 服务
我将 Flask 与 gevent-socketio 一起使用: $ cat requirements.txt Flask==0.10.1 Jinja2==2.7.1 MarkupSafe==0.18
我正在为某个项目使用 netty-socketio。我像这样设置了一个服务器: Configuration config = new Configuration(); config.setHostna
所以,我需要收集很多数据,比如每秒 3 个单位的数据,由很多人收集。我将收集此数据的事件命名为“捕获”。 我正在使用 JWT 对其他数据密集度较低的事件进行身份验证。 现在,我只是担心使用每个数据密集
socketio不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: git地址: https://github.com/fen
我正在使用 flask-socketio 创建一个聊天应用程序,现在我想在客户端断开连接时使用客户端的用户名向服务器发送一条消息,即当客户端关闭浏览器/选项卡时,以便我可以将该消息从服务器发送到所有现
我正在尝试向从单独文件连接的所有套接字发出。但是我似乎无法弄清楚。 Socket.js var socketio = require('socket.io'); var users = require
我尝试在我的 django 应用程序中实现与 django-socketio 的聊天。 首先要了解django-socketio,所以看了https://github.com/stephenmcd/d
我对网络编程有点陌生。我现在正在尝试编写一个服务器。在服务器端我有一个方法: socket.on("joinserver", function(name, device) { }); 在客户端,
我正在尝试将数据从客户端发送到服务器端。我只得到未定义。我希望用户单击 ledOn 函数来发送 1 ,并单击 ledOff 来发送 0 到服务器。 server.js io.sockets.on('c
我使用nodejs设置了一个简单的Socket服务器gameIo gameIo.on("connection", socket => { socket.on("sign up", () => {
我想知道是否有一种方法可以在两个(或多个)flask-socketio 服务器之间直接通信。我想在服务器之间传递信息,并让客户端连接单个 Web 套接字服务器,该服务器将具有来自其他服务器的所有组合逻
我是一名优秀的程序员,十分优秀!