heroku-python / flask-sockets

[DEPRECATED] Alternative: https://github.com/miguelgrinberg/flask-sock
MIT License
1.74k stars 167 forks source link

Possible to use to proxy websockets? #72

Open buddyroo30 opened 4 years ago

buddyroo30 commented 4 years ago

I'm trying to create a reverse proxy in Python, and got it working for the normal HTTP traffic, but can't figure out how to proxy web sockets. I was hoping flask_sockets could do this, but I can't figure out how to do it. Is it possible, and how would you do that? I tried to do like this, e.g. (using, in addition, websocket):

@sockets.route('/2/') @sockets.route('/2/') def proxy_socket(ws_in): path = ws_in.environ['QUERY_STRING'] message = ws_in.receive() ws_path = f'{SITE_NAME2_WS}{path}' ws = create_connection(ws_path) ws.send(message) result = ws.recv() while not ws.closed: ws_in.send(result) ws.close()

But this didn't work, I'm able to read messages sent on ws_in, but it fails to connect and send them to the backend. Anyway, if you have any guidance on how to proxy websockets in Python, I'd appreciate it. Thanks.

nmz787 commented 3 years ago

the lack of indentation makes your code snippet basically illegible