Reputation: 351
I'm trying to send data to server from client but I get bad namespace error.
I searched for the error but didn't find any solution... Can anyone point out what mistake I've done.
server side (app.py)
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
socket_app = SocketIO(app)
@socket_app.on('userdata', namespace='/test')
def username(data):
print(data)
print(request.sid)
if __name__ == '__main__':
socket_app.run(app, debug=True, host='127.0.0.1', port=5000)
client side(client.py)
import socketio
sio = socketio.Client(engineio_logger=True)
data = {}
data['name'] = 'Mark'
sio.connect('http://127.0.0.1:5000')
sio.emit('userdata', data , namespace='/test')
sio.wait()
error
Attempting polling connection to http://127.0.0.1:5000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': '1ipsM7udDVMj0WdKAAAA', 'upgrades': ['websocket'], 'pingTimeout': 5000, 'pingInterval': 25000}
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to ws://127.0.0.1:5000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Traceback (most recent call last):
File "client.py", line 41, in <module>
sio.emit('userdata', data , namespace='/test')
File "/home/amit/anaconda3/envs/ANPR/lib/python3.6/site-packages/socketio/client.py", line 329, in emit
namespace + ' is not a connected namespace.')
socketio.exceptions.BadNamespaceError: /test is not a connected namespace.
Received packet NOOP data
flask & socketio versions
python-socketio==5.0.4 python-engineio==4.0.0 Flask==1.0.2 Flask-SocketIO==5.0.1
Upvotes: 0
Views: 2411
Reputation: 310
It looks like the OP figured out their own issue but when connecting to a non-default namespace you can specify it in the client connection:
sio.connect('http://127.0.0.1:5000', namespaces=['/test'])
Upvotes: 1
Reputation: 351
I got this working by having the connect
handler invoked in server.
@socket_app.on('connect', namespace='/test')
def test_connect():
print("connected")
emit('connect_custom', {'data': 'Connected'})
which then emits to client's connect_custom
handler.
@sio.on('connect_custom', namespace='/test')
def on_connect(data):
print('client contacted by server')
print(data)
data = {}
data['name'] = 'Mark'
sio.emit('userdata', data, namespace='/test')
sio.connect('http://127.0.0.1:5000')
Upvotes: 0