3
현재 작은 대화를 작성 중입니다.Node.js + socket.io는 이벤트에 대한 데이터를 반환합니다.
누군가가 엔터를 누르면 서버에 메시지를 제출하고 삽입이 성공하면 상태 정보를 반환해야합니다.
모든 이벤트에서 일부 데이터를 반환하는 방법이 있습니까, 아니면 상태를 클라이언트에게 알리기 위해 서버에서 다른 보내기를 시작해야합니까?
클라이언트
$('textarea').keypress(function(e) {
if(e.which == 13) {
var msg = $.trim(this.value);
if(msg.length > 0) {
socket.send(msg, function() {
// Here I need to have some result
// returned by the server to notify the user
// if everything went fine
//this.value = '';
});
}
return false;
}
});
서버
socket.on('message', function() {
db.query("INSERT INTO chat (message, ip, time) VALUES (?, INET_ATON(?), NOW())", [msg, socket.handshake.headers['x-forwarded-for'] || socket.handshake.address.address], function(error) {
if(!error) {
io.sockets.send(msg);
} else {
console.log(error);
}
});
});