简体   繁体   English

非阻塞输入提示? node.js

[英]Non blocking input prompt? node.js

I am working on a chat app using socket.io/node.js, in the terminal,I am able to chat with another session, but when I send a message to in order to read the message I have the either hit enter on the prompt, or type a message, not the best. 我正在使用socket.io/node.js的聊天应用程序,在终端中,我可以与另一个会话进行聊天,但是当我向我发送消息以阅读消息时,在提示,或键入一条消息,不是最好的。 Any alternatives to console.log()? 可以使用console.log()替代吗?

The console.log containing the nick and message variables, is fired when the new message event is received, any alternatives to make it more realtime? 当收到新的消息事件时,会触发包含nick和message变量的console.log,是否有其他替代方法可以使它更实时? I am using the Prompt-Sync module. 我正在使用提示同步模块。

EDIT: on message event snippet. 编辑:消息事件摘要。

On message function: 开消息功能:

        socket.on('msg',function(data){ 

         var newmessage = data.m;
         var newmsgnick = data.nick;
         newmessagefunc(newmessage,newmsgnick)

        });

newmessagefunc function: newmessagefunc函数:

        function newmessagefunc(message,nick){
         console.log(nick+": "+message);
         message = i("message: ")
            }

The message log: 消息日志:

 message: Hello - how goes?
 Ozzie: Hello - how goes?
 message: Hello - ?
 Ozzie: it goes well. 
 message: 

To read the message the other user sent, I have to either submit a message, or just enter on the message prompt, they do not show as they are sent, as socket.io is said to be able to do, any ways around this? 要阅读其他用户发送的消息,我必须提交一条消息,或者只是在消息提示符下输入消息,它们不会像发送时那样显示,因为据说socket.io可以做到这一点?

You are reading a message when there is an incoming message. 当有传入消息时,您正在阅读消息。 This is forcing you to type a message before you display the incoming message. 这迫使您在显示收到的消息之前先键入一条消息。

socket.on('msg',function(data){ 
  var newmessage = data.nm;
  var newmsgnick = data.nn;
  console.log(newmessage);
});

Now, this will print the incoming message. 现在,这将打印收到的消息。 Keep the prompt code outside of this block. 将提示代码保留在此块之外。

while(typeof message === "undefined"){
  message = i("message: ") 
}
socket.emit('msg', {'nm':message, 'nn':'nick'});

The native prompt dialog is blocking. 机提示对话框被阻止。 Meaning the rest of the script, even when new events are received, will not run until it is unblocked. 这意味着脚本的其余部分,即使收到新事件,也要等到取消阻止后才能运行。

Most will use a textbox to get user input, and on a button click or similar event to grab the input and post the message. 大多数将使用文本框来获取用户输入,并在按钮上单击或类似事件来获取输入并发布消息。 This way the socket msg event can be triggered without it being blocked 这样, socket msg事件可以被触发而不会被阻止

HTML 的HTML

<input id="message"><button id="sendmsgbtn">Send</button>

JS JS

var msgInput = document.getElementById("message");
var btn = document.getElementById("sendmsgbtn");

btn.addEventListener("click,sendMessage);
msgInput.addEventListener("keyup",function(e){
    //this is used to detect if there was a enter 
    //key press inside the input
    if(e.keyCode==13){
        sendMessage();
    }
});

socket.on('msg',onMsg);    

function onMsg(data){
    var newmessage = data.nm;
    var newmsgnick = data.nn;
    console.log(newmessage);
    //add message to chat window
}

function sendMsg(){
    var msg = msgInput.value;
    msgInput.value = "";
    socket.emit("msg",{msg:msg});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM