Skip to content

Commit

Permalink
Automated merge with ssh://bitbucket.org/stephenmcd/django-socketio
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmcd committed Feb 4, 2012
2 parents fabe006 + 88f3bab commit eb4d098
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -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/
.. _`low port`: http://www.staldal.nu/tech/2007/10/31/why-can-only-root-listen-to-ports-below-1024/
1 change: 1 addition & 0 deletions django_socketio/example_project/chat/views.py
Expand Up @@ -6,6 +6,7 @@

from chat.models import ChatRoom, ChatUser


@events.on_subscribe
def message(request, socket, context, channel):
print "subscribed to " + channel
Expand Down
14 changes: 14 additions & 0 deletions django_socketio/templates/socketio_scripts.html
Expand Up @@ -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]);
Expand Down
4 changes: 3 additions & 1 deletion django_socketio/templatetags/socketio_tags.py
Expand Up @@ -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
14 changes: 11 additions & 3 deletions django_socketio/views.py
Expand Up @@ -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)
Expand Down

0 comments on commit eb4d098

Please sign in to comment.