D
D
durnevdanya2017-03-09 21:15:09
JavaScript
durnevdanya, 2017-03-09 21:15:09

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?
2uKH0r8qfds.jpgclient

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>');
      });
    }

server
socket.on('sendMessage', function(data){
    io.sockets.emit('backMsg', data);
  });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2017-03-09
@durnevdanya

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 question

Ask a Question

731 491 924 answers to any question