客户代码:
$(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时,首先点击,显示一个消息,两次点击,显示两个消息,三次点击,显示三个消息...