Answer the question
In order to leave comments, you need to log in
Node.JS why post is duplicated?
Hello everyone, I made a chat, but for some reason, when sending messages, two messages are sent at once, and then three, four, five ...
What's the problem? client
var socket = io();
function sendMsg() {
var $msg = $("#message");
var $chat = $("#chat");
socket.emit('sendMessage', $msg.val());
socket.on('backMsg', function(data){
$chat.append('<div class="well">'+data+'</div>');
});
}
socket.on('sendMessage', function(data){
io.sockets.emit('backMsg', data);
});
Answer the question
In order to leave comments, you need to log in
You hang up a handler every time you send socket.on('backMsg', function(data){
it. You only need to do it once.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question