I'm trying to run a flask-socketio server that connects to a react app in the browser.
python Server code (relevant portions):
from flask import Flask, request, jsonify from flask_socketio import SocketIO app = Flask(__name__) socketio = SocketIO(app) @socketio.on('message') def handle_message(message): print('received message: ' + message) if __name__ == '__main__': socketio.run(app) client js(running in browser):
var ws = new WebSocket("ws://127.0.0.1:5000"); console.log('connecting to log ws') ws.onopen = function (event) { console.log('connection opened') }; ws.onmessage = function (event) { console.log(event.data); self.recieve(event.data); } I get the following error in the browser:
failed: Error during WebSocket handshake: Unexpected response code: 404 Now I've heard that this is often because I've made a mistake in the url I use. However, when I change
var ws = new WebSocket("ws://127.0.0.1:5000");
to
var ws = new WebSocket("wss://127.0.0.1:5000");
I can see that the server gets the connection request, but it returns the following error on the server side:
127.0.0.1 - - [02/Nov/2017 13:07:59] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00t\x01\x00\x00p\x03\x01Y\xfb{\x1f\x0 5\x8d\xbf|\xdb\xeb\x12\xe8\xf6\xb0\xf9') What do i need to do to get a successful connection set up here? The browser used here is chrome, but Im having difficulty with others as well.