-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomet.py
32 lines (30 loc) · 1.14 KB
/
comet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
import time
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado_utils.routes import route
from tornado.web import authenticated
from handlers import NoticeHandler
from tornado.escape import json_decode,json_encode
from tornado.httpclient import HTTPError
@route(r'/update')
class MainHandler(NoticeHandler):
def get_data(self,callback):
[unread_data ,count] = self.noti.get_unread(self.current_user.uid,page_size=10);
unread = self.parse_notis(unread_data)
callback([unread,count])
@tornado.web.asynchronous
@authenticated
def post(self,*m,**kw):
#TODO: timeout 30
atonce = bool( self.get_argument('atonce',default=False))
if atonce :
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 1, lambda: self.get_data(callback=self.wait))
else :
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 10, lambda: self.get_data(callback=self.wait))
def get(self,*m,**kw):
raise HTTPError(405)
def wait(self, result):
self.json_write(code='201',data=result)
self.finish()