2015-08-08 93 views
0

我們正在構建一個消息系統,可以直接與用戶聊天。由於我們使用的是Rails3,並且4的升級即將到來,所以我們將使用輪詢而不是流式處理。PollingPattern丟失了部分數據

我們遇到了問題,可能是由設計模式造成的。

function OurChat() { 
    this.timestamp = null 
    var self = this 
    this.init = function() { 
     self.timestamp = (new Date().getTime()).toString().substr(0, 10) //rails conform 

於是我們使輪詢,其中頻率是5000毫秒

this.polling = function() { 

    $.getJSON("/chat.json?t=" + self.timestamp, function(data) { 
     self.set_timestamp() 

     self.update_friends(data.friends) 
     self.update_messages(data.messages) 

     window.setTimeout(function() { 
      self.polling(); 
     }, self.frequency); 
    });   
} 

如果我們撰寫郵件,我們將請求發送到服務器,並顯示與所述類消息「.temporary」

當我們輪詢時,我們將消息與臨時類一起刪除,並將消息添加到我們的消息列表中。 這裏的問題是開始。

last_polling_timestamp is **1000** 
[...4.5 seconds are gone...] 
-> user sends a message -> request takes whatever it takes 
[..5seconds are gone..] 
-> poling is looking for messages > timestamp **1000** 
    -> _blank result_ 
[..little bit later..] 
-> message is created by server, timestamp **1001** 
[..5seconds are later..] 
-> poling is looking for messages > timestamp **1005** 
    -> _blank result_ 

我們怎麼解決這個問題? 我最早的想法是發回每條消息的ID,將ID保存在.message [data-id]處。然後在過去總是10秒內輪詢(以便每個請求應該已經存在)。檢查所有消息,並查看它是否已經在我的domtree中。如果沒有 - >插入它(在正確的oder中)。 是更好的方法嗎?

回答

0

我們現在禁用了輸入字段,只要後期請求沒有完成。