我已经完成了socket.io聊天室的教程。我希望能够通过键入/mute *person-name*
在聊天室中静音某人(不再接收聊天),然后通过键入/unmute *person-name*
取消静音,并通过键入/whisper *person-name*
关于如何做到这一点的任何想法?
答案 0 :(得分:0)
试试这个
要处理静音命令,请使用字符串操作来检查消息的第一个字母是否为“/”,以及命令中是否存在“静音”。您需要一个用户列表来循环检查谁被静音。
socket.on('chat message', function(msg) {
if(msg.charAt(0) == "/") {
//Process command
command = msg.substring(1, msg.length);
//unmute
if (command.includes("unmute")) {
for (var i = 0; i < your_user_list.length; i++) {
if (command.includes(your_user_list[i].username)) {
your_user_list[i].muted = false;
}
}
} //mute
else if (command.includes("mute")) {
for (var i = 0; i < your_user_list.length; i++) {
if (command.includes(your_user_list[i].username)) {
your_user_list[i].muted = true;
}
}
}
}
else {
//Send message
console.log(socket.username + ": " + msg);
io.emit('chat message', socket.username + ": " + msg);
}
});
要静音某人将一个成员变量添加到套接字
socket.muted = true;
然后在每次提交此类消息时检查用户是否静音。
socket.on('chat message', function(msg) {
if(!socket.muted) {
//Allow the user to chat
if(msg.charAt(0) == "/") {
//Process command
command = msg.substring(1, msg.length);
//unmute
if (command.includes("unmute")) {
for (var i = 0; i < your_user_list.length; i++) {
if (command.includes(your_user_list[i].username)) {
your_user_list[i].muted = false;
}
}
} //mute
else if (command.includes("mute")) {
for (var i = 0; i < your_user_list.length; i++) {
if (command.includes(your_user_list[i].username)) {
your_user_list[i].muted = true;
}
}
}
}
else {
//Send message
console.log(socket.username + ": " + msg);
io.emit('chat message', socket.username + ": " + msg);
}
}
else {
//Tell them they are muted
}
});