Permalink
Browse files

Utilise newest gevent-socketio

  • Loading branch information...
1 parent 5d64c3c commit 0d2cdb52c3f16d959bbd384cf3feb5e5383efc27 @jstasiak committed Apr 21, 2012
Showing with 40 additions and 48 deletions.
  1. +2 −2 README.rst
  2. +3 −2 realtime/events.py
  3. +1 −1 realtime/management/commands/rungevent.py
  4. +34 −43 realtime/views.py
View
@@ -17,7 +17,7 @@ Requirements
------------
* gevent-socketio from `my gevent-socketio repository <https://github.com/jstasiak/gevent-socketio>`_
- (``old`` branch) and its dependencies
+ (commit ``8835a91dffba4447564ffa30df95663a13e1997e``) and its dependencies
* pip
* django >= 1.3
* git ;)
@@ -26,7 +26,7 @@ In Red Hat/CentOS/Fedora they can be obtained by following commands::
yum install libevent-devel python-setuptools gcc git-core
easy_install pip
- pip install git+https://jstasiak@github.com/jstasiak/gevent-socketio.git@old
+ pip install git+https://jstasiak@github.com/jstasiak/gevent-socketio.git@8835a91dffba4447564ffa30df95663a13e1997e@
You also want to have Socket.IO client, such client in version 0.8.4 is provided by this project.
View
@@ -3,10 +3,11 @@ class Event(object):
Represents socket event received from client.
'''
- def __init__(self, socket, name, args = None, id = None):
+ def __init__(self, socket, name, namespace = None, args = None, id = None):
self._socket = socket
self.name = name
self.args = args
+ self._namespace = namespace
self._event_id = id
self._acknowledged = False
@@ -29,6 +30,6 @@ def ack(self, *args):
assert self.acknowledgeable()
assert not self.acknowledged()
- self._socket.ack(self._event_id, *args)
+ self._namespace.ack(*args, event_id = self._event_id)
self._acknowledged = True
@@ -17,7 +17,7 @@ def exception_printer(sender, **kwargs):
class Command(BaseCommand):
args = '[interface:port]'
- help = 'runs gevent-socketio pywsgi server'
+ help = 'runs gevent-socketio SocketIOServer'
def handle(self, *args, **options):
if len(args) == 1:
View
@@ -4,60 +4,51 @@
from django.db import transaction
from django.utils.translation import ugettext as _
+from socketio import socketio_manage
+from socketio.namespace import BaseNamespace
+
from .events import Event
from .signals import socket_connected, socket_disconnected, socket_client_event, socket_client_message, socket_client_event_by_type
-@transaction.commit_manually
def socketio_handler(request):
- try:
- socket = request.environ['socketio']
- connection = Connection(request = request, socket = socket)
- connection.handle()
- except Exception as e:
- transaction.rollback()
- raise
-
- return HttpResponse()
-
-
-class Connection(object):
- def __init__(self, request, socket):
- self._request = request
- self._socket = socket
-
- def handle(self):
- socket = self._socket
- request = self._request
+ value = socketio_manage(request.environ, {
+ '': RootNamespace,
+ }, request = request,
+ )
- socket_connected.send(sender = socket, request = request)
+ return value
- while True:
- message = socket.receive()
+class RootNamespace(BaseNamespace):
+ def recv_connect(self):
+ socket_connected.send(sender = self.socket,
+ request = self.request)
- # all socket handling runs inside single long-running transaction
- # commit refreshes transaction state
- transaction.commit()
+ def __getattr__(self, name):
+ assert name.startswith('on_')
+ name = name.lstrip('on_')
+ def f(*args, **kwargs):
+ event_id = kwargs.get('event_id')
+ with transaction.commit_on_success():
+ self.handle_event(name = name, args = args, event_id = event_id)
- if message:
- self.handle_message(message)
- else:
- break
+ return f
- transaction.commit()
+ def recv_disconnect(self):
+ socket_disconnected.send(sender = self.socket,
+ request = self.request)
- socket_disconnected.send(sender = socket, request = request)
+ def handle_event(self, name, args, event_id = None):
+ request, socket = self.request, self.socket
- def handle_message(self, message):
- request, socket = self._request, self._socket
- message_type = message.pop('type')
+ message_type = name
if message_type == 'message':
- socket_client_message.send(sender = socket, request = request, message = message['data'])
- elif message_type == 'event':
- event = Event(socket = socket, name = message['name'], args = message.get('args'),
- id = message.get('id'))
- socket_client_event.send(sender = socket, request = request, event = event)
- socket_client_event_by_type[event.name].send(sender = socket, request = request, event = event)
+ socket_client_message.send(sender = socket,
+ request = request, message = args)
else:
- assert False, "Should not happen"
-
+ event = Event(socket = socket, namespace = self, name = name,
+ args = args, id = event_id)
+ socket_client_event.send(
+ sender = socket, request = request, event = event)
+ socket_client_event_by_type[event.name].send(
+ sender = socket, request = request, event = event)

0 comments on commit 0d2cdb5

Please sign in to comment.