miguelgrinberg / Flask-SocketIO

Socket.IO integration for Flask applications.
MIT License
5.37k stars 892 forks source link

Default path "/socket.io" produces 404 #1106

Closed SharkFourSix closed 4 years ago

SharkFourSix commented 5 years ago

Describe the bug When connecting to the flask-websocket from a webpage serverd by flask, I get a 404 as if the path does not exist.

To Reproduce

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config['database']['uri']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
socketio = SocketIO(app)

@app.route('/')
def index():
    from flask import render_template
    return render_template('websocket_test.html')

@socketio.on('create')
def on_create():
    print('Created connection')

if __name__ == '__main__':
    kwargs = {
        'port': 2020,
        'debug': True
    }
socketio.run(app, host='0.0.0.0', **kwargs)

Expected behavior Should connect as expected.

Logs Client

Response Headers

HTTP/1.1 404 NOT FOUND Content-Type: application/json Content-Length: 58 Date: Mon, 18 Nov 2019 22:51:00 GMT

Request Headers

GET ws://localhost:2020/socket.io HTTP/1.1 Host: localhost:2020 Connection: Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket Origin: http://localhost:2020 Sec-WebSocket-Version: 13 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9,es;q=0.8 Sec-WebSocket-Key: QbJoXgu1KNvtKqN6+X5KYA== Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

Requirements.txt Flask==1.1.1 Flask-Migrate==2.5.2 Flask-SocketIO Flask-SQLAlchemy==2.4.0 future==0.17.1 gevent==1.4.0 greenlet==0.4.15 gunicorn==19.9.0 python-engineio python-socketio==4.3.1 Click==7.0

miguelgrinberg commented 5 years ago

What client are you using for this?

SharkFourSix commented 5 years ago

I want to use Android but I'm testing with Google Chrome first

miguelgrinberg commented 5 years ago

You are using gevent, which does not have native websocket support. Install gevent-websocket and try again, I believe that is what you are missing.

Also, the bug template asked you to include server-side logs as well. Pretty sure there is a big warning in there telling you that WebSocket is not enabled due to missing the above package.

SharkFourSix commented 5 years ago

I'm not getting any other server-side logs

socketio = SocketIO(app, logger=True, engineio_logger=True, debug=True)

After installing gevent-websocket I can connect but the connection gets closed right away:

So the following two scenarios happen. Notice the URL paths at the logs

SCENARIO 1


url ws://localhost:2020/socket.io

Log

INFO:engineio.server:Server initialized for gevent.
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 101-823-667

(After sometime and after the connection has already been reported as severed on the client side)

INFO:engineio.server:4a5959fc59a544e28f59be78b54cb439: Client is gone, closing socket
INFO:engineio.server:4a5959fc59a544e28f59be78b54cb439: Client is gone, closing socket

Nothing else is logged

Request headers GET ws://localhost:2020/socket.io HTTP/1.1 Host: localhost:2020 Connection: Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket Origin: http://localhost:2020 Sec-WebSocket-Version: 13 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9,es;q=0.8 Sec-WebSocket-Key: N3rtNf6GBl62ebHEPMbdHA== Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

Response headers HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: wbddXzjTai4E/UOONT1p5IMcSWY=

SCENARIO 2


url ws://localhost:2020/socket.io/create

Log

INFO:engineio.server:Server initialized for gevent.
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 101-823-667
INFO:engineio.server:06c0cb63855941e78796c721a8f0e6a8: Sending packet OPEN data {'sid': '06c0cb63855941e78796c721a8f0e6a8', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 25000}
INFO:engineio.server:06c0cb63855941e78796c721a8f0e6a8: Sending packet MESSAGE data 0

(After sometime)

INFO:engineio.server:06c0cb63855941e78796c721a8f0e6a8: Client is gone, closing socket
INFO:engineio.server:06c0cb63855941e78796c721a8f0e6a8: Client is gone, closing socket

Request headers GET ws://localhost:2020/socket.io/create HTTP/1.1 Host: localhost:2020 Connection: Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket Origin: http://localhost:2020 Sec-WebSocket-Version: 13 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9,es;q=0.8 Sec-WebSocket-Key: V12t0jeguHyUglIpfiz/Hw== Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

Response headers HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: r6I63gqLPqAIkZHhLrwFOdYOJJk=

I feel like there might be more missing stuff.

miguelgrinberg commented 4 years ago

Are you using a Socket.IO client? It does not look like you are, the ws://localhost:2020/socket.io/create URL isn't a valid Socket.IO URL.

SharkFourSix commented 4 years ago

No I'm using plain old native WebSocket API. And yes, it's not valid. That was my way of trying to figure things out.

So does this mean I'm only restricted to using Socket.IO libraries and can't use anything else?

miguelgrinberg commented 4 years ago

You are not restricted to anything, but if you use Socket.IO on the backend, then you have to use Socket.IO on the frontend as well. If you prefer WebSocket, then use a WebSocket server.

SharkFourSix commented 4 years ago

Aah. Thank you very much sir. That makes sense. I was under the impression that Socket.IO is high level and runs on top of WebSocket

After using Socket.IO, I can now connect successfully. So this was more of a misunderstanding than an actual issue.

Thank you for the quick replies and taking your time man.

miguelgrinberg commented 4 years ago

Well, you are correct. Socket.IO is built on top of WebSocket (and HTTP). A plain WebSocket client does not know how to connect.