2013-05-27 57 views
3

我正在嘗試安裝socket.io聊天服務器。我已經得到它來發送和接收來自服務器和客戶端的消息。我有興趣向用戶展示是否有人打字。我知道當有人從客戶端輸入到服務器時,我可以發射它,也可以將它廣播給其他用戶。但是在每個關鍵點上做到這一點會有效嗎?我如何處理這種情況?下面是我的代碼現在: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()}); 
    } 
}); 

,並在服務器端:

​​

回答

15

您可以使用超時送「啓動類型」和「停止打字」的消息,像這樣:

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

} 

這很粗糙,但你應該明白。

+0

一個更好的解釋:http://tamas.io/further-additions-to-the-node-jssocket-io-chat-app/ – gfivehost

+0

有用的2016年!沒有什麼可以解釋的權利! –

0
function typingtimeoutFunction(){     

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

    onKeyDownNotEnter: function(){ 
     clearTimeout(this.typingtimeout); 
     socket.emit('typingMessage', 'istyping'); 
     this.typingtimeout = setTimeout(function() {ajaxChat.typingtimeoutFunction();}, 5000);  
    }, 
0

只是想我會添加我的變化對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(); 
     } 
    }); 
}); 
相關問題