Commit f324d13e authored by Denis Bilenko's avatar Denis Bilenko

webchat: use class instead of global variables

parent 18754f7e
...@@ -6,51 +6,57 @@ from django.http import HttpResponse ...@@ -6,51 +6,57 @@ from django.http import HttpResponse
from gevent.event import Event from gevent.event import Event
from webchat import settings from webchat import settings
messages = []
MESSAGES_SIZE = 200
new_message_event = Event()
class ChatRoom(object):
cache_size = 200
def new_message(from_, body): def __init__(self):
data = {'id': str(uuid.uuid4()), 'from': from_, 'body': body} self.cache = []
data['html'] = render_to_string('message.html', dictionary={'message': data}) self.new_message_event = Event()
return data
def last_message_id(): def main(self, request):
if messages: if self.cache:
return messages[-1]['id'] request.session['cursor'] = self.cache[-1]['id']
return render_to_response('index.html', {'MEDIA_URL': settings.MEDIA_URL, 'messages': self.cache})
def main(request): def message_new(self, request):
if messages:
request.session['cursor'] = messages[-1]['id']
return render_to_response('index.html', {'MEDIA_URL': settings.MEDIA_URL, 'messages': messages})
def message_new(request):
name = request.META.get('REMOTE_ADDR') or 'Anonymous' name = request.META.get('REMOTE_ADDR') or 'Anonymous'
msg = new_message(name, request.POST['body']) msg = create_message(name, request.POST['body'])
messages.append(msg) self.cache.append(msg)
if len(messages) > MESSAGES_SIZE: if len(self.cache) > self.cache_size:
del messages[0] self.cache = self.cache[-self.cache_size:]
new_message_event.set() self.new_message_event.set()
new_message_event.clear() self.new_message_event.clear()
return json_response(msg) return json_response(msg)
def message_updates(request): def message_updates(self, request):
cursor = request.session.get('cursor') cursor = request.session.get('cursor')
if cursor == last_message_id(): if not self.cache or cursor == self.cache[-1]['id']:
new_message_event.wait() self.new_message_event.wait()
assert cursor != last_message_id(), cursor assert cursor != self.cache[-1]['id'], cursor
try: try:
for index, m in enumerate(messages): for index, m in enumerate(self.cache):
if m['id'] == cursor: if m['id'] == cursor:
return json_response({'messages': messages[index+1:]}) return json_response({'messages': self.cache[index+1:]})
return json_response({'messages': messages}) return json_response({'messages': self.cache})
finally: finally:
if messages: if self.cache:
request.session['cursor'] = messages[-1]['id'] request.session['cursor'] = self.cache[-1]['id']
else: else:
request.session.pop('cursor', None) request.session.pop('cursor', None)
room = ChatRoom()
main = room.main
message_new = room.message_new
message_updates = room.message_updates
def create_message(from_, body):
data = {'id': str(uuid.uuid4()), 'from': from_, 'body': body}
data['html'] = render_to_string('message.html', dictionary={'message': data})
return data
def json_response(value, **kwargs): def json_response(value, **kwargs):
kwargs.setdefault('content_type', 'text/javascript; charset=UTF-8') kwargs.setdefault('content_type', 'text/javascript; charset=UTF-8')
return HttpResponse(simplejson.dumps(value), **kwargs) return HttpResponse(simplejson.dumps(value), **kwargs)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment