From a4add05e3e917ac0f4a3045ae24477dd47d82a7f Mon Sep 17 00:00:00 2001 From: Stephen McDonald Date: Sun, 5 Feb 2012 05:49:40 +1100 Subject: [PATCH 1/2] Fall back to the port in settings if none provided in the environment. Closes #15. --- django_socketio/templatetags/socketio_tags.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django_socketio/templatetags/socketio_tags.py b/django_socketio/templatetags/socketio_tags.py index e1f9826..096897f 100644 --- a/django_socketio/templatetags/socketio_tags.py +++ b/django_socketio/templatetags/socketio_tags.py @@ -3,10 +3,12 @@ from django.template import Library +from django_socketio.settings import PORT + register = Library() @register.inclusion_tag("socketio_scripts.html", takes_context=True) def socketio(context): - context["DJANGO_SOCKETIO_PORT"] = environ["DJANGO_SOCKETIO_PORT"] + context["DJANGO_SOCKETIO_PORT"] = environ.get("DJANGO_SOCKETIO_PORT", PORT) return context From 88f3bab668bc05993edb4c231b585c9f87821b02 Mon Sep 17 00:00:00 2001 From: Stephen McDonald Date: Sun, 5 Feb 2012 05:51:25 +1100 Subject: [PATCH 2/2] Wrap client-side send() in a check for array, and pack them into an object that can be checked for on the server, so that arrays can be handled consistently, as Socket.IO sends each value in the array as an individual message. --- README.rst | 2 +- django_socketio/example_project/chat/views.py | 1 + django_socketio/templates/socketio_scripts.html | 14 ++++++++++++++ django_socketio/views.py | 14 +++++++++++--- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 46f6900..8c8df92 100644 --- a/README.rst +++ b/README.rst @@ -297,4 +297,4 @@ higher is required for the demo as it makes use of Django 1.3's .. _`pip`: http://www.pip-installer.org/ .. _`setuptools`: http://pypi.python.org/pypi/setuptools .. _`Flash Policy Server`: http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html -.. _`low port`: http://www.staldal.nu/tech/2007/10/31/why-can-only-root-listen-to-ports-below-1024/ \ No newline at end of file +.. _`low port`: http://www.staldal.nu/tech/2007/10/31/why-can-only-root-listen-to-ports-below-1024/ diff --git a/django_socketio/example_project/chat/views.py b/django_socketio/example_project/chat/views.py index 1fbe74c..613e34a 100644 --- a/django_socketio/example_project/chat/views.py +++ b/django_socketio/example_project/chat/views.py @@ -6,6 +6,7 @@ from chat.models import ChatRoom, ChatUser + @events.on_subscribe def message(request, socket, context, channel): print "subscribed to " + channel diff --git a/django_socketio/templates/socketio_scripts.html b/django_socketio/templates/socketio_scripts.html index b631642..fcda646 100644 --- a/django_socketio/templates/socketio_scripts.html +++ b/django_socketio/templates/socketio_scripts.html @@ -18,6 +18,20 @@ io.Socket.prototype[name] = prototype[name]; } + // Arrays are transferred as individual messages in Socket.IO, + // so we put them into an object and check for the __array__ + // message on the server to handle them consistently. + var send = io.Socket.prototype.send; + io.Socket.prototype.send = function(data) { + if ($.isArray(data)) { + channel = data[0] == '__subscribe__' || data[0] == '__unsubscribe__'; + if (!channel) { + data = ['__array__', data]; + } + } + return send.call(this, data); + }; + // Set up the subscription methods. io.Socket.prototype.subscribe = function(channel) { this.send(['__subscribe__', channel]); diff --git a/django_socketio/views.py b/django_socketio/views.py index 422d60e..4ad38ea 100644 --- a/django_socketio/views.py +++ b/django_socketio/views.py @@ -57,19 +57,27 @@ def socketio(request): # for these. messages = iter(messages) for message in messages: - if MESSAGE_LOG_FORMAT is not None: - formatted = format_log(request, message) - socket.handler.server.log.write(formatted) if message == "__subscribe__": chan = messages.next() socket.subscribe(chan) events.on_subscribe.send(request, socket, context, chan) + message = "[subscribe] %s" % chan elif message == "__unsubscribe__": chan = messages.next() socket.unsubscribe(chan) events.on_unsubscribe.send(request, socket, context, chan) + message = "[unsubscribe] %s" % chan else: + # Socket.IO transfers arrays as individual messages, so + # they're put into an object in socketio_scripts.html + # and given the __array__ key so that they can be treated + # consistently here. + if message == "__array__": + message = messages.next() events.on_message.send(request, socket, context, message) + if MESSAGE_LOG_FORMAT is not None: + formatted = format_log(request, message) + socket.handler.server.log.write(formatted) except Exception, exception: print_exc() events.on_error.send(request, socket, context, exception)