duck
duck

Reputation: 1894

Flask socketIO connection established but not routed

For my project I have to connect one socketIO backend to another. For this I am using Flask-socketio and socketio-client. The code for both is the following:

CLIENT:

from socketIO_client import SocketIO, LoggingNamespace
ip = '192.168.1.41'
port = 8090

def handle_aaa_response():
    print('response')

socketIO = SocketIO(ip, port)
socketIO.on('pingSuccess', on_aaa_response)
socketIO.wait(seconds=1)

SERVER:

from flask import Flask, render_template, jsonify, Response
from flask_socketio import SocketIO, emit

TRACE_LIBRARIES = False
HOST = '0.0.0.0'
WEB_PORT = 8090
USE_PIN = False

def handle_connect():
        print('hello world')
        emit('pingSuccess')

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
socketio = SocketIO(app, cors_allowed_origins="*")
socketio.on('connect', handle_connect)

try:
    socketio.run(app,
                    host=HOST,
                    port=WEB_PORT,
                    log_output=True)
except KeyboardInterrupt:
    print('*** User raised KeyboardInterrupt')
    exit()

When i run the client and server, the server only logs the following:

(4743) accepted ('192.168.1.33', 53500)
192.168.1.33 - - [21/Oct/2020 15:48:31] "GET /socket.io/?EIO=3&transport=polling&t=1603291711742-0 HTTP/1.1" 200 371 0.005033
(4743) accepted ('192.168.1.33', 53502)

This means the server is accepting the connection from the client, but not routing to the correct route on the server.

I want to know how I can change this so it gets to the correct route and prints "hello world:

Upvotes: 0

Views: 92

Answers (2)

Vishal Patel
Vishal Patel

Reputation: 507

Server Side

@socketio.on('connect')
def test_connect():
    print('hello world')
    emit('pingSuccess')

Upvotes: 0

BStadlbauer
BStadlbauer

Reputation: 1285

Contrary to socketio.on() from the regular socketIO_client package you use in your client script, flask_socketio uses .on() as a decorator.

So to add a callback to an event in flask_socketio, you would need to change the following:

...
socketio = SocketIO(app, cors_allowed_origins="*")


@socketio.on('connect')
def handle_connect():
    print('hello world')
    emit('pingSuccess')
...

Upvotes: 1

Related Questions