Socket.io how to check if user is typing and broadcast it to other users

后端 未结 4 462

I am trying to setup socket.io chat server. I have gotten it up to send and receive message from server and client. I was interested in showing users if some one is typing. I kn

4条回答
  •  无人共我
    2021-02-02 03:41

    You can use timeouts to send "started typing" and "stopped typing" messages, like so:

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

    It's rough, but you should get the idea.

提交回复
热议问题