Skip to content

Commit

Permalink
allow clients to specify a custom socket.io resource name
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 16, 2014
1 parent 6996e66 commit 3f6744f
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class GenericNamespace(base_namespace):
base_emit = base_namespace.emit
base_send = base_namespace.send


def initialize(self):
self.rooms = set()

Expand Down Expand Up @@ -92,13 +91,15 @@ def recv_message(self, data):
if self.socketio.server is None:
self.socketio.server = self.environ['socketio'].server
app = self.request
return self.socketio._dispatch_message(app, self, 'message', [data])
return self.socketio._dispatch_message(app, self, 'message',
[data])

def recv_json(self, data):
if self.socketio.server is None:
self.socketio.server = self.environ['socketio'].server
app = self.request
return self.socketio._dispatch_message(app, self, 'json', [data])
return self.socketio._dispatch_message(app, self, 'json',
[data])

def emit(self, event, *args, **kwargs):
ns_name = kwargs.pop('namespace', None)
Expand All @@ -107,10 +108,12 @@ def emit(self, event, *args, **kwargs):
if broadcast or room:
if ns_name is None:
ns_name = self.ns_name
return self.socketio.emit(event, *args, namespace=ns_name, room=room)
return self.socketio.emit(event, *args, namespace=ns_name,
room=room)
if ns_name is None:
return self.base_emit(event, *args, **kwargs)
return request.namespace.socket[ns_name].base_emit(event, *args, **kwargs)
return request.namespace.socket[ns_name].base_emit(event, *args,
**kwargs)

def send(self, message, json=False, ns_name=None, callback=None,
broadcast=False, room=None):
Expand All @@ -120,9 +123,12 @@ def send(self, message, json=False, ns_name=None, callback=None,
return self.socketio.send(message, json, ns_name, room)
if ns_name is None:
return request.namespace.base_send(message, json, callback)
return request.namespace.socket[ns_name].base_send(message, json, callback)
return request.namespace.socket[ns_name].base_send(message,
json,
callback)

namespaces = dict( (ns_name, GenericNamespace) for ns_name in self.messages)
namespaces = dict((ns_name, GenericNamespace)
for ns_name in self.messages)
return namespaces

def _dispatch_message(self, app, namespace, message, args=[]):
Expand Down Expand Up @@ -168,14 +174,15 @@ def on_message(self, message, handler, namespace=''):
self.messages[namespace][message] = handler

def on(self, message, namespace=''):
if namespace in self.exception_handlers or self.default_exception_handler is not None:
if namespace in self.exception_handlers or \
self.default_exception_handler is not None:
def decorator(f):
def func(*args, **kwargs):
try:
f(*args, **kwargs)
except:
handler = self.exception_handlers.get(namespace,
self.default_exception_handler)
handler = self.exception_handlers.get(
namespace, self.default_exception_handler)
type, value, traceback = sys.exc_info()
handler(value)
self.on_message(message, func, namespace)
Expand Down Expand Up @@ -232,11 +239,9 @@ def run(self, app, host=None, port=None, **kwargs):
port = int(server_name.rsplit(':', 1)[1])
else:
port = 5000
# don't allow override of resource, otherwise allow SocketIOServer
# kwargs to be passed through
kwargs.pop('resource', None)
resource = kwargs.pop('resource', 'socket.io')
self.server = SocketIOServer((host, port), app.wsgi_app,
resource='socket.io', **kwargs)
resource=resource, **kwargs)
if app.debug:
# monkey patching is required by the reloader
from gevent import monkey
Expand All @@ -258,8 +263,10 @@ def emit(event, *args, **kwargs):
return request.namespace.emit(event, *args, **kwargs)


def send(message, json=False, namespace=None, callback=None, broadcast=False, room=None):
return request.namespace.send(message, json, namespace, callback, broadcast, room)
def send(message, json=False, namespace=None, callback=None, broadcast=False,
room=None):
return request.namespace.send(message, json, namespace, callback, broadcast,
room)


def join_room(room):
Expand Down

0 comments on commit 3f6744f

Please sign in to comment.