Javascript Node.JS仅发布字符串一次,并在停止绘制时将其删除
当用户开始在画布上绘图时,我试图打印用户的用户名。我现在有这个,但它会不断打印用户的用户名一遍又一遍。无论如何,我可以使它只打印一次,然后在他们停止后,用户的用户名将被删除# 这是我必须发送给用户正在绘制的套接字的代码Javascript Node.JS仅发布字符串一次,并在停止绘制时将其删除,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,当用户开始在画布上绘图时,我试图打印用户的用户名。我现在有这个,但它会不断打印用户的用户名一遍又一遍。无论如何,我可以使它只打印一次,然后在他们停止后,用户的用户名将被删除# 这是我必须发送给用户正在绘制的套接字的代码 function mouseDragged() { if (username == undefined || username == null) { alert("Please enter a username before dr
function mouseDragged() {
if (username == undefined || username == null) {
alert("Please enter a username before drawing!");
}
else {
fill(255);
noStroke();
ellipse(mouseX, mouseY, 20, 20);
// Send the mouse coordinates
sendmouse(mouseX, mouseY);
socket.emit('typing', username);
}
}
function sendmouse(xpos, ypos) {
//console.log("sendmouse: " + xpos + " " + ypos);
var data = {
x: xpos,
y: ypos
};
// Send that object to the socket
socket.emit('mouse', data);
}
然后,这是将数据从Server.js发送到套接字的代码
socket.on('typing', (username) => {
//console.log(username + "is typing...");
socket.broadcast.emit('typing', username);
});
然后这是我打印用户正在键入的代码
socket.on('typing', function (username) {
//Adds username to list if they type!
console.log("A user is typing!");
$('#typing').append($('<li>').text(username + "is drawing..."));
}, 1000);
socket.on('typing',函数(用户名){
//如果他们键入,则将用户名添加到列表中!
log(“用户正在键入!”);
$(“#键入”).append($(“”).text(用户名+“正在绘图…”);
}, 1000);
所以提醒一下,我想打印用户的用户名,就像现在一样。但我只希望它打印一次,然后在用户停止后,用户的用户名将被删除。
非常感谢您花时间阅读本书。:)