Permalink
Browse files

Upgrade to latest gevent-socketio

  • Loading branch information...
stephenmcd committed Jul 30, 2012
1 parent 566b97e commit 6e8b645f4b5ddca6417e4ee2c5cf25f278513073
Showing with 32 additions and 41 deletions.
  1. +22 −30 core/management/commands/runserver_socketio.py
  2. +3 −3 core/utils.py
  3. +3 −3 requirements.txt
  4. +4 −5 static/js/drawnby.js
@@ -2,30 +2,36 @@
from re import match
from thread import start_new_thread
from time import sleep
-from os import getpid, kill
-from signal import SIGINT
-from django.core.handlers.wsgi import WSGIHandler
from django.core.management.base import BaseCommand, CommandError
from django.core.management.commands.runserver import naiveip_re
-from django.utils.autoreload import code_changed, restart_with_reloader
-from socketio import SocketIOServer
+from socketio import socketio_manage
+from socketio.server import SocketIOServer
+from socketio.mixins import BroadcastMixin
+from socketio.namespace import BaseNamespace
+from core.utils import Actions
-RELOAD = False
-def reload_watcher():
- global RELOAD
- while True:
- RELOAD = code_changed()
- if RELOAD:
- kill(getpid(), SIGINT)
- sleep(1)
+class DrawingNamespace(BaseNamespace, BroadcastMixin):
+
+ def __init__(self, environ, ns_name, request=None):
+ super(DrawingNamespace, self).__init__(environ, ns_name, request)
+ self.actions = Actions(self)
+
+ def on_message(self, message):
+ broadcast = self.actions([str(s) for s in message])
+ if broadcast:
+ self.broadcast_event("message", message)
+
+
+def application(environ, start_response):
+ socketio_manage(environ, {"": DrawingNamespace})
+
class Command(BaseCommand):
def handle(self, addrport="", **kwargs):
-
if not addrport:
self.addr = "127.0.0.1"
self.port = 9000
@@ -35,19 +41,5 @@ def handle(self, addrport="", **kwargs):
raise CommandError('"%s" is not a valid port number '
'or address:port pair.' % addrport)
self.addr, _, _, _, self.port = m.groups()
-
- start_new_thread(reload_watcher, ())
- try:
- bind = (self.addr, int(self.port))
- print
- print "SocketIOServer running on %s:%s" % bind
- print
- server = SocketIOServer(bind, WSGIHandler(), resource="socket.io")
- server.serve_forever()
- except KeyboardInterrupt:
- if RELOAD:
- print
- print "Reloading..."
- restart_with_reloader()
- else:
- raise
+ server = SocketIOServer((self.addr, self.port), application)
+ server.serve_forever()
View
@@ -18,8 +18,8 @@ class Actions(object):
each drawing action.
"""
- def __init__(self, socket):
- self.socket = socket
+ def __init__(self, namespace):
+ self.namespace = namespace
self.handlers = ("join", "leave", "save", "mousedown")
def __call__(self, message):
@@ -50,7 +50,7 @@ def join(self, message):
for s in [self.drawing_key, "join"] + m.split(",")]
drawing_actions = [s for m in redis.lrange(self.drawing_data_key, 0, -1)
for s in m.split(",")]
- self.socket.send(user_actions + drawing_actions)
+ self.namespace.emit("message", user_actions + drawing_actions)
return True
def leave(self, message):
View
@@ -5,9 +5,9 @@ django-extensions==0.6
django-compressor==0.9.2
easy-thumbnails==1.0-alpha-17
django-pagination==1.0.7
-gevent==0.13.6
-gevent-socketio==0.2.1
-gevent-websocket==0.2.3
+gevent==0.13.7
+gevent-socketio==0.3.5-rc2
+gevent-websocket==0.3.6
redis==2.4.6
docutils==0.7
django-ratings==0.3.6
View
@@ -103,22 +103,21 @@ $(function() {
return args;
};
action(getArgs(arguments));
- socket.send(getArgs(arguments));
+ socket.emit('message', getArgs(arguments));
};
var save = function() {
var title = prompt('Save as:');
if (title) {
messages.add('Sketch saved');
- socket.send([window.drawingKey, 'save', title,
- canvas.get()[0].toDataURL('image/png')])
+ socket.emit('message', [window.drawingKey, 'save', title,
+ canvas.get()[0].toDataURL('image/png')])
}
dirty = false;
};
// Socket.IO setup.
- var socket = new io.Socket();
- socket.connect();
+ self.socket = io.connect(':9000');
socket.on('connect', function() {
send('join');
});

0 comments on commit 6e8b645

Please sign in to comment.