0

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.

0

1 Answer 1

1

I figured out the answer. You cannot use WebSocket in the browser to communicate with flask-socketio, instead I needed to also use regular socket.io in the client side to communicate with the server. Since switching, I'm getting perfect communication between client and server.

Sign up to request clarification or add additional context in comments.

1 Comment

It can be done using the browser, but if you're using a web based client you need to make sure it uses SocketIO, and not some other implementation. One I have found that works for is amritb.github.io/socketio-client-tool

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.