2015-07-10 44 views
1

實際上我必須在與socket.io的簡單節點聊天中實現讀取和不讀取狀態。節點js實時聊天讀取或不讀取狀態

我已經建立實時聊天應用程序的幫助節點和socket.io。 但現在我想要的是當用戶1發送消息到另一個用戶2user1的狀態爲if user2讀取或不讀取發送給其他用戶的消息,如whatsup現在所做的。

有關此狀態的任何想法。

回答

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!"; 
    }); 

我知道,這個代碼可能不是喊得讀/未讀狀態的最短途徑。但它工作完美。

+0

Thanx幫助我 –

+0

mg52感謝您的幫助腳本! 這是發送者將獲得消息已成功發送給接收者的狀態。 我在徘徊如何知道發件人發送信息的狀態是否已被接收者讀取? –