I have a basic chat that works fine, however when a user opens another page (/chat
and /profile
at same time) I get another connection, and same user will have duplicated sessions in the chat page.
What I am looking for a way to avoid this behavior. Maybe allow sockets only in /chat
page?
main.py
....
@socketio.on('message')
def handleMessage(msg):
print ('Message: ' , msg)
send(msg, broadcast=True)
chart.html
var socket = io.connect('http://192.168.56.10/');
socket.on('connect', function() {
socket.send('User has connected!');
});
socket.on('message', function(msg) {
$("#messages_chat").append('<li>' + msg + '</li>');
console.log("receive messages");
});
$("#sendbutton").on('click', function() {
socket.send($('#myMessage').val());
$('#myMessage').val('');
});
views.py
....
@mod.route('/chat', methods=['GET'])
@login_required
def chat():
return render_template("users/chat.html")