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/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 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)