Reputation: 27
this code is supposed to work, but it does not work
As soon as a client connects, the session 'keyo' is assigned to the value 'example'
I tried to make an equivalent of socket.myvariable with session['myvariable']
@app.route("/a")
def helljo():
return render_template('index.html')
@socketio.on('connect')
def handleMessagae():
session['keyo'] = request.args.get('session') # /a?session=example
emit('connected', session.get('keyo'))
@socketio.on('message')
def handleMessage(message):
emit('message', {'pseudo':session.get('keyo'),'message':message} , broadcast=True, include_self=False)
Upvotes: 2
Views: 3167
Reputation: 27
The Alternative Solution :
@app.route("/a", methods=['GET'])
def index():
resp = request.args.get('session')
return render_template( 'index.html' , resp=resp )
@socketio.on('My_Event')
def message(msg):
print(msg)
on index.html
<script>
// ...
{% if resp %}
variable = '{{ resp }}';
{% else %}
variable = 'null';
{% endif %}
socket.emit( 'My_Event' , variable );
// ...
</script>
Upvotes: 0
Reputation: 67489
The correct way to do what you want is:
@app.route("/a")
def helljo():
session['keyo'] = request.args.get('session') # /a?session=example
return render_template('index.html')
@socketio.on('connect')
def handleMessagae():
emit('connected', session.get('keyo'))
@socketio.on('message')
def handleMessage(message):
emit('message', {'pseudo':session.get('keyo'),'message':message} , broadcast=True, include_self=False)
The problem is that you are mixing up the HTTP request with the Socket.IO requests. If you invoke the /a
endpoint with some query string args, you can only access those arguments in the handler for that endpoint. But saving them in the session makes them accessible later by the Socket.IO event handlers.
Upvotes: 1
Reputation: 27
the fourth line of this code sends to all clients except to sender of the request. I would like to send it only to sender
@app.route("/a", methods=['GET'])
def helljo():
session['keyo'] = request.args.get('session')
socketio.emit('alert_sender', {'resp': session.get('keyo')})
return render_template('index.html')
@socketio.on('connect')
def handleMessagae():
emit('connected', 'hi')
@socketio.on('message')
def handleMessage(message):
emit('message', {'pseudo':session.get('keyo'),'message':message} , broadcast=True, include_self=False)
on index.html (when I go on /a?session=example)
...
socket.on('alert_sender', function(data) {
alert(data.resp);
// others receive alert : example sender receive : nothing
});
...
Upvotes: 0
Reputation:
@app.route("/a", methods=['GET'])
def helljo():
session['keyo'] = request.args.get('session')
socketio.emit('EVENT', {'var': session.get('keyo')})
return render_template('index.html')
@socketio.on('connect')
def handleMessagae():
emit('connected', session.get('keyo'))
@socketio.on('message')
def handleMessage(message):
emit('message', {'pseudo':session.get('keyo'),'message':message} , broadcast=True, include_self=False)
The problem here is, that you're trying to get request arguments within a socketio event, this isn't going to work as request arguments are only accessible inside of an @app.route()
function.
Upvotes: 0