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中)。 是更好的方法吗?