node js实时聊天已读或未读状态

node js real time chat read or not read status

实际上我必须在与 socket.io 的简单节点聊天中实现已读和未读状态。

我已经在节点和 socket.io 的帮助下构建了实时聊天应用程序。 但现在我想要的是 user1 向另一个 user2 发送消息。 user1 的状态为 if user2 已读或未读发送给另一个用户的消息,就像 whatsup 现在做的那样。

关于此状态的任何想法。

我使用 Nodejs 和 Socket.io 获得了一个绘制和发送应用程序,这是我的应用程序的存储库:https://github.com/mg52/DrawChat。该应用程序具有 sent/sending(它的工作方式类似于 read/not 读取)状态。在我的应用程序中,每个用户都有一个唯一的 ID。此 sent/sending 状态取决于这些唯一 ID。

例如,

在index.html中:

当您点击发送按钮时,

message_sent.innerHTML = "Message Send Status: Sending..."

socket.on("get_message", function (data) {
//getting message from sender (from index.js)
socket.emit("message_sent",{sentuserid:data.userid});

});

在index.js中:

socket.on("message_sent", function(data){
    io.sockets.emit("message_sent_correctly",{msc:data.sentuserid});
    //it sends back to all users (includes sender) the sender's id 
});

在index.html中:

socket.on("message_sent_correctly",function(data){
    socket.emit("check_id", {ci: data.msc});
    //sends back to server to control who has sent the message
});

在index.js中:

socket.on("check_id",function(data){
    if(data.ci === user.uid){
        socket.emit("id_checked");
    }
    //if id equals to sender's id returns to index.html that the message has been sent successfully
});

在index.html中:

socket.on("id_checked",function(){
     message_sent.innerHTML = "Your message has been read!";
    });

我知道此代码可能不是处理 read/not 读取状态的最短方法。但它完美无缺。