0
votes

I'm trying to create an app that uses SocketIO, I'm using Flask-socketio and using Socket.io Client Tool to test the connection of the socket. I've done it as shown in the flask-socketio's documentation but I am not able to connect the socket. I get the following errors while connecting:

Client Error: enter image description here

Server Side Error: enter image description here

The code:

app = Flask(__name__)
CORS(app)
app.config.from_object(Config)
socketio = SocketIO(app)

jwt = JWTManager(app)
db = MongoEngine(app)
api = Api(app)


@app.route('/')
def messageReceived(methods=None):
    if methods is None:
        methods = ['GET', 'POST']
    print('message was received!!!')


# @socketio.on('connect', namespace='/test')
# def test_connect():
#     print('Client connected')
#
#
# @socketio.on('disconnect', namespace='/test')
# def test_disconnect():
#     print('Client disconnected')


@socketio.on('my event')
def handle_my_custom_event(json, methods=None):
    if methods is None:
        methods = ['GET', 'POST']
    print('received my event: ' + str(json))
    socketio.emit('my response', json, callback=messageReceived)


if __name__ == '__main__':
    app.run(debug=True, host='localhost', port=5000)
    socketio.run(app, logger=True, engineio_logger=True)
1

1 Answers

3
votes

The Flask-SocketIO server enforces a same-origin policy by default. You need to configure the origin from your Socket.IO test tool as an allowed origin. For example:

socketio = SocketIO(app, cors_allowed_origins="https://amritb.github.io")