10

socket.io チャット サーバーをセットアップしようとしています。サーバーとクライアントからメッセージを送受信できるようになりました。誰かが入力しているかどうかをユーザーに示すことに興味がありました。クライアントからサーバーに入力しているときに出力できることを知っています。他のユーザーにブロードキャストすることもできます。しかし、すべてのキーアップでそれを行うのは効率的でしょうか? このような状況をどのように処理しますか?以下は今のところ私のコードです:

$("#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});
});
4

5 に答える 5

32

タイムアウトを使用して、次のように「入力を開始しました」および「入力を停止しました」メッセージを送信できます。

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

}

大雑把ですが、アイデアを得る必要があります。

于 2013-05-27T09:22:05.727 に答える
1

abject_error が提供する非常に役立つ回答にバリエーションを追加すると思いました。これにより、入力のステータスと、入力を停止したがメッセージが入力されたことが可能になります。私はsocket.ioを使用していませんが、アイデアはわかります。

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();
        }
    });
});
于 2016-07-02T13:35:49.217 に答える
0
   function typingtimeoutFunction (){                 

      socket.emit('typingMessage', 'stopped typing');    
    },

    onKeyDownNotEnter: function (){
         clearTimeout(this.typingtimeout);
         socket.emit('typingMessage', 'istyping');
        this.typingtimeout = setTimeout(function() {ajaxChat.typingtimeoutFunction();}, 5000);      
    },
于 2015-02-11T07:45:18.650 に答える