nodejs socket.io chat,点击按钮累积触发器发射

时间:2018-06-17 08:55:22

标签: node.js socket.io

客户代码:

$(document).off('click.chatBtn').on('click.chatBtn','.chatBtn',function(e){
    var $target = $(e.currentTarget);
    var $charInput = $target.siblings('.charInput');
    var val = $charInput.val();
    var userName = $userName.val();
    if(!userName){
        alert('please input name');
        return;
    }
    socket = io.connect();
    socket.emit('chat',userName+"说:"+val);
    socket.on('chat',function(data){
        var $it = addMsg(data);
        $it.appendTo($message);
    });
});

服务器代码:

 socket.on('chat',function(data){
    io.sockets.emit('chat',data);
});

当我点击chatBtn时,首先点击,显示一个消息,两次点击,显示两个消息,三次点击,显示三个消息...

enter image description here

0 个答案:

没有答案