R
Railway10mo ago
A3lx

Hello everyone, i'm trying to make a python websocket using websockets library also flask

so basically i have this ->
async def main():
url = os.environ.get('RAILWAY_URL', 'localhost')
port = os.environ.get('RAILWAY_PORT')
print(f"WebSocket server is running at: wss://{url}:{port}")
asyncio.ensure_future(ping_pong())
asyncio.ensure_future(websockets.serve(handle_connection, "0.0.0.0", 4505))

app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))

asyncio.run(main())
async def main():
url = os.environ.get('RAILWAY_URL', 'localhost')
port = os.environ.get('RAILWAY_PORT')
print(f"WebSocket server is running at: wss://{url}:{port}")
asyncio.ensure_future(ping_pong())
asyncio.ensure_future(websockets.serve(handle_connection, "0.0.0.0", 4505))

app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))

asyncio.run(main())
functions functions etc.... when i go to my website hhr-bot-api.xyz i get the empty flask page but the thing is how do i connect to the websocket server? the railway terminal doesn't show anything out of common i used everything. wss://hhr-bot-api.xyz:4505 / ....... nothing looks to work.
15 Replies
Percy
Percy10mo ago
Project ID: fe5eb4db-8d92-4c45-b324-2d7bdeba2691
Brody
Brody10mo ago
you can't expose multiple ports, you need to have the websocket and http server listening on a single port
A3lx
A3lx10mo ago
okay i'll try this one. Thanks
A3lx
A3lx10mo ago
doesn't work either
Brody
Brody10mo ago
what doesn't work
A3lx
A3lx10mo ago
connecting to the websocket the flask app works perfectly
Brody
Brody10mo ago
then that would be a code issue
A3lx
A3lx10mo ago
btw site -> https://hhr-bot-api.xyz/ i get no errors or anything else
Brody
Brody10mo ago
that's an issue too
A3lx
A3lx10mo ago
Pastebin
import loggingimport flask;from flask import Flask, render_template...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
A3lx
A3lx10mo ago
look by yourself ....
Brody
Brody10mo ago
unfortunately I'm not a python developer, so I'm not much help here, but I know for an absolute fact that this can be done
A3lx
A3lx10mo ago
oh ty anyway then.
jonbeau
jonbeau10mo ago
some things that might help, you may consider using quart instead of flask: https://flask.palletsprojects.com/en/2.3.x/async-await/#when-to-use-quart-instead i connect to my websocket like this (no port)
let host = window.location.hostname;
let socket = new WebSocket(`wss://${host}/ws/con`);
let host = window.location.hostname;
let socket = new WebSocket(`wss://${host}/ws/con`);
and it seems to be working, but im still in testing phases. a fetch interrupts this connection & that may be a problem with my code. in quart you can define a websocket route like this
@app.websocket('/ws/con')
async def websock():
pass
@app.websocket('/ws/con')
async def websock():
pass
A3lx
A3lx10mo ago
thanks, works now.