Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 43b0799b70fef7257c0f2d2f3ce0f1835e430afb @peterbe committed Sep 6, 2012
No changes.
@@ -0,0 +1,53 @@
+from optparse import make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand
+from django.utils.importlib import import_module
+from tornado import web, ioloop
+from sockjs.tornado import SockJSRouter
+
+
+class Command(BaseCommand):
+
+ option_list = BaseCommand.option_list + (
+ make_option(
+ '--port',
+ action='store',
+ dest='port',
+ default=getattr(settings, 'SOCKJS_PORT', 9999),
+ help='What port number to run the socket server on'),
+ make_option(
+ '--no-keep-alive',
+ action='store_true',
+ dest='no_keep_alive',
+ default=False,
+ help='Set no_keep_alive on the connection if your server needs it')
+ )
+
+ def handle(self, **options):
+ if len(settings.SOCKJS_CLASSES) > 1:
+ from django.core.exceptions import ImproperlyConfigured
+ raise ImproperlyConfigured(
+ "Multiple connections not yet supported"
+ )
+
+ module_name, cls_name = settings.SOCKJS_CLASSES[0].rsplit('.', 1)
+ module = import_module(module_name)
+ cls = getattr(module, cls_name)
+ channel = getattr(settings, 'SOCKJS_CHANNEL', '/echo')
+ if not channel.startswith('/'):
+ channel = '/%s' % channel
+
+ router = SockJSRouter(cls, channel)
+ app_settings = {
+ 'debug': settings.DEBUG,
+ }
+
+ PORT = int(options['port'])
+ app = web.Application(router.urls, **app_settings)
+ app.listen(PORT, no_keep_alive=options['no_keep_alive'])
+ print "Running sock app on port", PORT, "with channel", channel
+ try:
+ ioloop.IOLoop.instance().start()
+ except KeyboardInterrupt:
+ # so you don't think you errored when ^C'ing out
+ pass
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+import os
+import sys
+
+if __name__ == "__main__":
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
+
+ from django.core.management import execute_from_command_line
+
+ execute_from_command_line(sys.argv)
No changes.
No changes.
@@ -0,0 +1,12 @@
+import datetime
+from django.db import models
+from django.utils.timezone import utc
+
+def now():
+ return datetime.datetime.utcnow().replace(tzinfo=utc)
+
+
+class Message(models.Model):
+ name = models.CharField(max_length=200)
+ message = models.TextField()
+ date = models.DateTimeField(default=now)
@@ -0,0 +1,32 @@
+import json
+from sockjs.tornado import SockJSConnection
+from .models import Message
+
+
+class ChatConnection(SockJSConnection):
+ _connected = set()
+
+ def on_open(self, request):
+ #print "OPEN"
+ #print request.get_cookie('name')
+ self._connected.add(self)
+ for each in Message.objects.all().order_by('date')[:10]:
+ self.send(self._package_message(each))
+
+ def on_message(self, data):
+ data = json.loads(data)
+ #print "DATA", repr(data)
+ msg = Message.objects.create(
+ name=data['name'],
+ message=data['message']
+ )
+ self.broadcast(self._connected, self._package_message(msg))
+
+ def on_close(self):
+ #print "CLOSE"
+ self._connected.remove(self)
+
+ def _package_message(self, m):
+ return {'date': m.date.strftime('%H:%M:%S'),
+ 'message': m.message,
+ 'name': m.name}
Oops, something went wrong.

0 comments on commit 43b0799

Please sign in to comment.