cache_size = 500
def __init__(self):
+ logger.info("initializing")
self.user = None
self.user_cache = {}
self.user_cursor = {}
assert(self.new_message_user_event[user])
except:
self.new_message_user_event[user] = Event()
- # self.new_message_user_event[user] = Event()
try:
if self.user_cache[user]:
self.user_cursor[user] = self.user_cache[user][-1]['id']
self.user_cursor[user] = self.user_cache[user][-1]['id']
else:
self.user_cursor[user] = self.user_cache[user][-2]['id']
-# self.cache.append(msg)
if len(self.user_cache[user]) > self.cache_size:
self.user_cache[user] = self.user_cache[user][-self.cache_size:]
self.new_message_user_event[user].set()
if request.is_ajax():
cursor = {}
try:
- # user = request.user.username
user = request.user.get_profile().peer.domain_name
except:
user = None
except:
self.user_cache[user] = []
if not self.user_cache[user] or cursor[user] == self.user_cache[user][-1]['id']:
- self.new_message_user_event[user].wait()
- # self.new_message_event.wait()
- # assert cursor[user] != self.user_cache[user][-1]['id'], cursor[user]
+ self.new_message_user_event[user].wait(settings.POLL_SESSION_UPDATE)
try:
for index, m in enumerate(self.user_cache[user]):
if m['id'] == cursor[user]:
if self.user_cache[user]:
self.user_cursor[user] = self.user_cache[user][-1]['id']
return HttpResponseRedirect(reverse('group-routes'))
- # else:
- # request.session.pop('cursor', None)
def monitor_polls(self, polls=None):
b = beanstalkc.Connection()
p.spawn(self.monitor_polls)
msgs = Msgs()
-
main = msgs.main
message_new = msgs.message_new
poll = msgs.start_polling
poll()
-
-
-
-
-
-
-
-
-
$("#message").select();
{% if user.is_authenticated %}
updater.start();
- updater.keepalive();
+ updater.poll();
{% endif %}
});
}
jQuery.postJSON = function(url, args, callback) {
- $.ajax({url: url, dataType: "text", type: "POST",
+ $.ajax({url: url, dataType: "json", type: "POST",
success: function(response) {
- if (callback) callback(eval("(" + response + ")"));
+ if (callback) callback(response);
}, error: function(response) {
- console.log("ERROR:", response)
+ console.log("ERROR:", response);
}});
};
var updater = {
errorSleepTime: 500,
cursor: null,
- xhrlp: null,
- keepalivetime: 120000,
- keepalive: function (){
- try {
- updater.xhrlp.abort();
- }
- catch (e) {
- }
- updater.poll();
- if (updater.errorSleepTime == 500){
- window.setTimeout(updater.keepalive, updater.keepalivetime);
- }
- else{
- window.setTimeout(updater.keepalive, updater.keepalivetime+updater.errorSleepTime);
- }
- },
-
start: function() {
- $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "text",
+ $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "json",
success: updater.onFetchExisting,
error: updater.onError});
},
poll: function() {
{% if user.is_authenticated %}
- if (updater.errorSleepTime > 60000){
- window.setTimeout('location.reload()', 1000);
- }
- updater.xhrlp=$.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "text",
+ if (errorSleepTime > 128000){
+ window.setTimeout('location.reload()', 500);
+ }
+ timeout = {{timeout}};
+ $.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "json",
success: updater.onSuccess,
+ timeout: timeout,
error: updater.onError});
{% endif %}
},
onSuccess: function(response) {
try {
- updater.newMessages(eval("(" + response + ")"));
+ updater.newMessages(response);
} catch (e) {
updater.onError();
return;
onFetchExisting: function(response) {
try {
- updater.existingMessages(eval("(" + response + ")"));
+ updater.existingMessages(response);
+
} catch (e) {
-// updater.onError();
+ updater.onError();
return;
}
},
onError: function(response, text) {
- if (text != 'abort'){
- updater.errorSleepTime *= 2;
- console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
- window.setTimeout(updater.keepalive, updater.errorSleepTime);
+ if (text == 'timeout'){
+ window.setTimeout('location.reload()', 3000);
}
+ updater.errorSleepTime *= 2;
+ console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
+ window.setTimeout(updater.poll, updater.errorSleepTime);
+
},
newMessages: function(response) {
if (!response.messages) return;
+ if (response.messages.length == 0){
+ return true;
+ }
updater.cursor = response.cursor;
var messages = response.messages;
updater.cursor = messages[messages.length - 1].id;
-// console.log(messages.length, "new messages, cursor:", updater.cursor);
+ console.log(messages.length, "new messages, cursor:", updater.cursor);
for (var i = 0; i < messages.length; i++) {
updater.showMessage(messages[i]);
existingMessages: function(response) {
if (!response.messages) return;
+ if (response.messages.length == 0){
+ return true;
+ }
updater.cursor = response.cursor;
var messages = response.messages;
updater.cursor = messages[messages.length - 1].id;