Flask SocketIO send message from server to room
In my app
, I need the client
to join a room
so that it may then receive
messages from my server
.
Server Code
@socketio.on('join', namespace='/test')
def join(message):
join_room(message['room'])
room = message['room']
emit('my response', {'data': 'Entered the room ' + message['room']}, room=room)
@app.route('/scan/user/<int:user_id>/venue/<int:venue_id>', methods = ['POST'])
@auth.login_required
def scan_tablet_user_func(user_id, venue_id):
room = 'venue_' + str(venue_id)
socketio.emit('my response', {'data': json.dumps(my_info, ensure_ascii=False)}, room=room)
Client Code
$('form#join').submit(function(event) {
socket.emit('join', {room: $('#join_room').val()});
return false;
});
As soon as the webpage
loads, I enter "venue_1"
into the webpage form
entitled "join"
.
The variable
room
on the server
side is also set to "venue_1"
.
The issue is that when I call the API
/scan/user/...
, nothing appears on my client
. However,
emit('my response', {'data': 'Entered the room ' + message['room']}, room=room)
does appear correctly.
I think that you need to supply a namespace parameter to your emit in the API call, since you have defined a namespace.
socketio.emit('my response', {'data': json.dumps(my_info, ensure_ascii=False)}, room=room, namespace='/test')
If you don't supply the namespace you will use the default one.
链接地址: http://www.djcxy.com/p/86454.html