Node.js io如何检查用户是否正在键入并将其广播给其他用户
我正在尝试设置socket.io聊天服务器。我已经准备好从服务器和客户端发送和接收消息。我很想向用户展示是否有人在打字。我知道,当一些用户从客户端到服务器键入内容时,我可以发出消息,我还可以将消息广播给其他用户。但在每一次关键时刻这样做是否有效?我如何处理这种情况?以下是我目前的代码:Node.js io如何检查用户是否正在键入并将其广播给其他用户,node.js,socket.io,Node.js,Socket.io,我正在尝试设置socket.io聊天服务器。我已经准备好从服务器和客户端发送和接收消息。我很想向用户展示是否有人在打字。我知道,当一些用户从客户端到服务器键入内容时,我可以发出消息,我还可以将消息广播给其他用户。但在每一次关键时刻这样做是否有效?我如何处理这种情况?以下是我目前的代码: $("#textbox").keyup(function (e) { if (e.keyCode == 13) { socket.emit('send', {nickname: $('#nickname
$("#textbox").keyup(function (e) {
if (e.keyCode == 13) {
socket.emit('send', {nickname: $('#nickname').val() , msg: $("#textbox").val()});
$('#textbox').val(''); }
else{
socket.emit('is typing', {nickname: $('#nickname').val()});
}
});
在服务器端:
socket.on('is typing', function(data){
socket.broadcast.emit('typing', {nickname: data.nickname});
});
您可以使用超时发送“开始键入”和“停止键入”消息,如下所示:
var typing = false;
var timeout = undefined;
function timeoutFunction(){
typing = false;
socket.emit(noLongerTypingMessage);
}
function onKeyDownNotEnter(){
if(typing == false) {
typing = true
socket.emit(typingMessage);
timeout = setTimeout(timeoutFunction, 5000);
} else {
clearTimeout(timeout);
timeout = setTimeout(timeoutFunction, 5000);
}
}
这很粗糙,但你应该明白了。我只是想把我的变体添加到“卑鄙的错误”给出的非常有用的答案上。这允许输入输入状态和已停止输入状态,但已输入消息。我没有使用socket.io,但你明白了
function typingtimeoutFunction (){
socket.emit('typingMessage', 'stopped typing');
},
onKeyDownNotEnter: function (){
clearTimeout(this.typingtimeout);
socket.emit('typingMessage', 'istyping');
this.typingtimeout = setTimeout(function() {ajaxChat.typingtimeoutFunction();}, 5000);
},
var typingStatus=0; //0=Not typing: message blank, 1=typing, 2=typing: something there
var typingStatus_timeout = undefined;
//called when timeout kicks in that we've stopped typing
function function_typingStatus_timeout(){
//message entered
if ($('#chat_message').val().trim()) {
typingStatus=2;
//message is blank
} else {
typingStatus=0;
}
//send new status
connection.send(JSON.stringify({
action:'typingStatus',
typingStatus:typingStatus,
}));
}
$(document).ready(function(){
//TYPING
$('#chat_message').keydown(function(e) {
//only record typing if enter not pressed
if(e.which!=13 && e.keyCode!=13) {
//weren't typing before but are now
if (typingStatus!=1) {
//switch to 1 for in progress
typingStatus=1;
//broadcast
connection.send(JSON.stringify({
action:'typingStatus',
typingStatus:typingStatus,
}));
//were typing before and still are, clear the timeout, we're going to reset it in the next step
} else {
clearTimeout(typingStatus_timeout);
}
//either way, set the timeout to trigger after a second of not typing
typingStatus_timeout=setTimeout(function_typingStatus_timeout,1000);
//if enter was pressed we want to not have it register as a keydown at all. there's a separate function on keyup that looks for enter being pressed and sends the message
} else {
e.preventDefault();
}
});
});
只需按照本教程在此处添加我的代码:
//建立连接
var socket=io.connect('http://localhost:4000');
变量超时
//查询DOM
var message=document.getElementById('message'),
handle=document.getElementById('handle'),
btn=document.getElementById('send'),
output=document.getElementById('output'),
反馈=document.getElementById('feedback');
//发出事件
btn.addEventListener('click',function(){
socket.emit('chat'{
message:message.value,
句柄:handle.value
});
message.value=“”;
});
函数timeoutFunction(){
socket.emit(“键入”,false);
}
message.addEventListener('keyup',function(){
emit('typing',handle.value);
clearTimeout(超时)
超时=设置超时(timeoutFunction,2000)
})
//倾听事件
socket.on('chat',函数(数据){
feedback.innerHTML='';
output.innerHTML+=''+data.handle+':'+data.message+'';
});
socket.on('typing',函数(数据){
如果(数据){
feedback.innerHTML=''+data+'正在键入…';
}否则{
feedback.innerHTML=“”
}
});
注意它如何不需要输入布尔变量我应该在哪里调用
onkeydownotenter
??
// Make connection
var socket = io.connect('http://localhost:4000');
var timeout
// Query DOM
var message = document.getElementById('message'),
handle = document.getElementById('handle'),
btn = document.getElementById('send'),
output = document.getElementById('output'),
feedback = document.getElementById('feedback');
// Emit events
btn.addEventListener('click', function(){
socket.emit('chat', {
message: message.value,
handle: handle.value
});
message.value = "";
});
function timeoutFunction() {
socket.emit("typing", false);
}
message.addEventListener('keyup',function(){
socket.emit('typing', handle.value);
clearTimeout(timeout)
timeout = setTimeout(timeoutFunction, 2000)
})
// Listen for events
socket.on('chat', function(data){
feedback.innerHTML = '';
output.innerHTML += '<p><strong>' + data.handle + ': </strong>' + data.message + '</p>';
});
socket.on('typing', function(data){
if (data) {
feedback.innerHTML = '<p><em>' + data + ' is typing...</em></p>';
} else {
feedback.innerHTML = ''
}
});