简体   繁体   English

Heroku websocket 到 websocket 通讯

[英]Heroku websocket to websocket communication

I'm trying to implement an invite system for my game using Heroku and websockets.我正在尝试使用 Heroku 和 websockets 为我的游戏实现邀请系统。 I have tried the official Heroku example but it doesn't work.我已经尝试过官方的 Heroku 示例,但它不起作用。

Here is the basic app:这是基本应用程序:

from flask import Flask
from flask-sock import Sock

app = Flask(__name__)
sock = Sock(app)

messages = []

@app.route('/')
def index():
    return "Nothing here..."

@sock.route("/submit")
def inbox(ws):
    while True:
        msg = ws.receive()
        ws.send("Message received.")
        messages.append(msg)

@sock.route("/receive")
def outbox(ws):
    while True:
        for msg in messages:
            ws.send(msg)

Even something as simple as that doesn't work, and from the tests I've run it seems like it's because each websocket connection is getting it's own instance of messages , and it seems like in the Heroku example it's the same deal with self.clients .即使像这样简单的东西也不起作用,从我运行的测试来看,似乎是因为每个 websocket 连接都在获取它自己的messages实例,并且在 Heroku 示例中似乎与self.clients

Here's my requirements.txt:这是我的requirements.txt:

click==8.1.3
colorama==0.4.5
Flask==2.2.2
flask-sock==0.5.2
gunicorn==20.1.0
h11==0.13.0
importlib-metadata==4.12.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
passlib==1.7.4
psycopg2-binary==2.9.3
simple-websocket==0.8.0
Werkzeug==2.2.2
wsproto==1.1.0
zipp==3.8.1
requests==2.26.0
redis==4.3.4
gevent==21.12.0

and my Procfile:和我的 Procfile:

web: gunicorn app:app --timeout 200

and my runtime.txt:和我的runtime.txt:

python-3.9.6

What I want is essentially a way to send data from one websocket to another without having to query a database constantly.我想要的本质上是一种无需不断查询数据库即可将数据从一个 websocket 发送到另一个的方法。 Any help is greatly appreciated.任何帮助是极大的赞赏。

You have imported flask_sock package not flask_socket package.您已导入 flask_sock package 而不是 flask_socket package。 this is confusing ik there are other packages websockets, socketio and they have similar syntaxes.这很令人困惑,因为还有其他包 websockets、socketio 并且它们具有相似的语法。 try making the backenf from websockets to deploy it easily on heroku.尝试从 websockets 制作 backenf,以便在 heroku 上轻松部署它。

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

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