Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make it compatible with the most recent gevent-socketio

  • Loading branch information...
commit bf7a90da0986945e89c35776356cc9d8a7042917 1 parent 75ebaf0
@jstasiak authored
View
11 README.rst
@@ -10,6 +10,8 @@ Please feel free to create an issue if you feel that something should be improve
or documentation. I also encourage you to hack the source code, it is pretty short and can provide
better understanding of what's going on.
+**This documentation is out of date**
+
Installation
============
@@ -26,10 +28,13 @@ 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@8835a91dffba4447564ffa30df95663a13e1997e@
You also want to have Socket.IO client, such client in version 0.9.6 is provided by this project.
+To install dependencies to use chat example please execute::
+
+ pip install -r requiements.txt
+
Installation itself
-------------------
@@ -176,10 +181,6 @@ from signal handler parameter ``sender`` or by other means), you can use followi
socket.broadcast_send('Hey! New user connected!')
socket.broadcast_emit('notice', 'Server is shutting down', 'kaboom')
- # acknowledges receiving of an event with particular id
- # signature: socket.ack(EVENT_ID, *args)
- socket.ack('13+', 'event', 'was', 'received', 'blah', 'blah')
-
In current implementation of ``gevent-socketio``, if message passed to ``socket.send`` is not
basestring instance, it will be converted to its string representation. There is no JSON
encoding here.
View
34 example_chat/chat/__init__.py
@@ -6,36 +6,37 @@
@receiver(socket_client_event_by_type['echo'])
def handle_echo(sender, request, event, **kwargs):
- if event.acknowledgeable():
- event.ack(*event.args)
+ return event.args
@receiver(socket_connected)
def handle_connected(sender, request, **kwargs):
socket = sender
session = socket.session
- session.user_name = None
+ del session['user_name']
@receiver(socket_disconnected)
def handle_disconnected(sender, request, **kwargs):
socket = sender
session = socket.session
- if session.user_name:
- socket.broadcast_emit('system_message', '{0} has left'.format(session.user_name))
+ namespace = kwargs['namespace']
+ if 'user_name' in session:
+ namespace.broadcast_event_not_me('system_message', '{0} has left'.format(session['user_name']))
@receiver(socket_client_event_by_type['chat_set_name'])
def handle_set_name(sender, request, event, **kwargs):
socket = sender
user_name = event.args[0]
+ namespace = kwargs['namespace']
session = socket.session
- if session.user_name:
- socket.broadcast_emit('system_message', '{0} changed his name to {1}'.format(
- session.user_name, user_name), include_self = True)
+ if 'user_name' in session:
+ namespace.broadcast_event('system_message', '{0} changed his name to {1}'.format(
+ session['user_name'], user_name))
else:
- socket.broadcast_emit('system_message', '{0} has joined'.format(user_name), include_self = True)
+ namespace.broadcast_event('system_message', '{0} has joined'.format(user_name))
- session.user_name = user_name
- event.ack(success())
+ session['user_name'] = user_name
+ return success()
@receiver(socket_client_event_by_type['chat_message'])
@@ -44,9 +45,12 @@ def handle_chat_message(sender, request, event, **kwargs):
session = socket.session
message = event.args[0]
+ namespace = kwargs['namespace']
- if not session.user_name:
- event.ack(failure())
+ if 'user_name' not in session:
+ result = failure()
else:
- socket.broadcast_emit('chat_message', session.user_name, message, include_self = True)
- event.ack(success())
+ namespace.broadcast_event('chat_message', session['user_name'], message)
+ result = success()
+
+ return result
View
24 realtime/events.py
@@ -9,27 +9,3 @@ def __init__(self, socket, name, namespace = None, args = None, id = None):
self.args = args
self._namespace = namespace
self._event_id = id
- self._acknowledged = False
-
- def acknowledgeable(self):
- '''
- Returns true if event can be acknowledged - it depends on specific socket.emit() call on client
- '''
- return bool(self._event_id)
-
- def acknowledged(self):
- '''
- Returns true if event has been already acknowledged.
- '''
- return self._acknowledged
-
- def ack(self, *args):
- '''
- Confirms reception of the event with optional data passed back to client.
- '''
- assert self.acknowledgeable()
- assert not self.acknowledged()
-
- self._namespace.ack(*args, event_id = self._event_id)
- self._acknowledged = True
-
View
2  realtime/management/commands/rungevent.py
@@ -10,7 +10,7 @@
from django.core.signals import got_request_exception
from django.utils import autoreload
-from socketio import SocketIOServer
+from socketio.server import SocketIOServer
def exception_printer(sender, **kwargs):
print_exc()
View
44 realtime/views.py
@@ -1,54 +1,68 @@
# -*- coding: utf-8 -*-
+import new
+
from django.http import HttpResponse
from django.db import transaction
from django.utils.translation import ugettext as _
from socketio import socketio_manage
+from socketio.mixins import BroadcastMixin
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
def socketio_handler(request):
- value = socketio_manage(request.environ, {
+ socketio_manage(request.environ, {
'': RootNamespace,
}, request = request,
)
- return value
+ return HttpResponse('')
-class RootNamespace(BaseNamespace):
+class RootNamespace(BroadcastMixin, BaseNamespace):
def recv_connect(self):
socket_connected.send(sender = self.socket,
- request = self.request)
+ request = self.request, namespace = self)
def __getattr__(self, name):
assert name.startswith('on_')
- name = name.lstrip('on_')
- def f(*args, **kwargs):
- event_id = kwargs.get('event_id')
+
+ def fun(self, packet):
+ name = packet['name']
+ args = packet['args']
+ event_id = packet.get('id')
with transaction.commit_on_success():
- self.handle_event(name = name, args = args, event_id = event_id)
+ value = self.handle_event(name = name, args = args, event_id = event_id)
+
+ return value
+
+ return new.instancemethod(fun, self, None)
- return f
def recv_disconnect(self):
socket_disconnected.send(sender = self.socket,
- request = self.request)
+ request = self.request, namespace = self)
def handle_event(self, name, args, event_id = None):
request, socket = self.request, self.socket
message_type = name
+ responses = []
if message_type == 'message':
- socket_client_message.send(sender = socket,
+ responses = socket_client_message.send(sender = socket,
request = request, message = args)
else:
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)
+ kwargs = dict(sender = socket, request = request, event = event,
+ namespace = self)
+ responses = socket_client_event.send(**kwargs)
+ responses.extend(socket_client_event_by_type[event.name].send(**kwargs))
+
+ responses = [response for (receiver, response) in responses if response]
+ if responses:
+ return responses[0]
+
View
2  requirements.txt
@@ -0,0 +1,2 @@
+gevent-socketio
+django
Please sign in to comment.
Something went wrong with that request. Please try again.