简体   繁体   English

从外部进程发出的 Flask-Socketio

[英]Flask-Socketio emitting from an external process

I've been working on a project using flask, flask-socketio and redis我一直在使用 flask、flask-socketio 和 redis 开发一个项目

I have a server, and some modules I would like to be able to emit from outside of the server file.我有一个服务器,我希望能够从服务器文件外部发出一些模块。

server.py服务器.py

from flask import Flask, Response, request, json
from flask_socketio import SocketIO, join_room, leave_room, emit

app = Flask(__name__)

socketio = SocketIO()

socketio.init_app(
    app, 
    cors_allowed_origins="*",
    message_que="redis://127.0.0.1:6379/0"
)

@socketio.on('ready')
def ready(data):
    socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')
...

jobque.py作业查询.py

from modules.server.server import socketio

...

socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')

As it's currently configured, emits from the server file all work, the clients respond and they can talk back and forth.按照当前的配置,从服务器文件发出所有工作,客户端响应并且他们可以来回交谈。 But when jobque goes to emit the same message, nothing happens.但是当jobque发出相同的消息时,什么也没有发生。 There's no error, and the client doesn't hear it.没有错误,客户也没有听到。

I'm also using redis for things other than the websockets, I can get and set from it with no problem, in both files.我也将 redis 用于 websockets 以外的其他东西,我可以在两个文件中毫无问题地从它获取和设置。

What do I need to do to get this external process to emit?我需要做什么才能让这个外部进程发出? I've looked through the flask-socketio documentation and this is exactly how they have it setup.我查看了flask-socketio文档,这正是他们的设置方式。

I've also tries creating a new SocketIO object inside jobque.py instead of importing the one form the server, but the results are the same我还尝试在 jobque.py 中创建一个新的 SocketIO jobque.py而不是从服务器导入一个,但结果是一样的

socket = SocketIO(message_queue="redis://127.0.0.1:6379/0")
socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')

I also went and checked if I could see the websocket traffic in redis with the message que setup using redis-cli > MONITOR , but I don't see any.我还去检查了我是否可以看到 redis 中的 websocket 流量,消息队列设置使用redis-cli > MONITOR ,但我没有看到任何。 I only see the operations I'm using redis for directly with the redis module.我只看到我使用 redis 直接使用 redis 模块的操作。 This makes me think the message que isn't actually being used, but I can't know for sure.这让我觉得消息队列实际上并没有被使用,但我不能确定。

Unfortunately spelled message_queue as message_que was the issue.不幸的是,将message_queue拼写为message_que是问题所在。 Creating a new SocketIO instance without the app works now.现在可以在没有应用程序的情况下创建新的 SocketIO 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM