简体   繁体   中英

Heroku websocket to websocket communication

I'm trying to implement an invite system for my game using Heroku and websockets. I have tried the official Heroku example but it doesn't work.

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 .

Here's my 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:

web: gunicorn app:app --timeout 200

and my 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. Any help is greatly appreciated.

You have imported flask_sock package not flask_socket package. this is confusing ik there are other packages websockets, socketio and they have similar syntaxes. try making the backenf from websockets to deploy it easily on heroku.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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