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

时间:2015-07-10 06:04:45

标签: node.js socket.io

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

我已经在节点和socket.io的帮助下构建了实时聊天应用程序。 但现在我想要的是当 user1 向另一个 user2 发送消息时。 user1 的状态为 user2 读取或未读取的消息,该消息会立即发送给其他用户,例如whatsup。

关于这种状态的任何想法。

1 个答案:

答案 0 :(得分:1)

我得到了一个平局并使用Nodejs和Socket.io发送应用程序,这是我的应用程序的存储库:https://github.com/mg52/DrawChat。应用程序已发送/发送(它的工作方式类似于读取/未读取)状态。在我的应用中,每个用户都有一个唯一的ID。此发送/发送状态取决于这些唯一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!";
    });

我知道这些代码可能不是处理读取/不读取状态的最短路径。但它很完美。

相关问题