|
@ -11,17 +11,13 @@ class Namespace(object): |
|
|
omitted, the default namespace is used. |
|
|
omitted, the default namespace is used. |
|
|
""" |
|
|
""" |
|
|
def __init__(self, namespace=None): |
|
|
def __init__(self, namespace=None): |
|
|
self.namespace = namespace or '/' |
|
|
self.name = namespace or '/' |
|
|
self.server = None |
|
|
self.server = None |
|
|
|
|
|
self.middlewares = [] |
|
|
|
|
|
|
|
|
def set_server(self, server): |
|
|
def set_server(self, server): |
|
|
self.server = server |
|
|
self.server = server |
|
|
|
|
|
|
|
|
def trigger_event(self, event, *args): |
|
|
|
|
|
handler_name = 'on_' + event |
|
|
|
|
|
if hasattr(self, handler_name): |
|
|
|
|
|
return getattr(self, handler_name)(*args) |
|
|
|
|
|
|
|
|
|
|
|
def emit(self, event, data=None, room=None, skip_sid=None, namespace=None, |
|
|
def emit(self, event, data=None, room=None, skip_sid=None, namespace=None, |
|
|
callback=None): |
|
|
callback=None): |
|
|
"""Emit a custom event to one or more connected clients. |
|
|
"""Emit a custom event to one or more connected clients. |
|
@ -31,7 +27,7 @@ class Namespace(object): |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.emit(event, data=data, room=room, skip_sid=skip_sid, |
|
|
return self.server.emit(event, data=data, room=room, skip_sid=skip_sid, |
|
|
namespace=namespace or self.namespace, |
|
|
namespace=namespace or self.name, |
|
|
callback=callback) |
|
|
callback=callback) |
|
|
|
|
|
|
|
|
def send(self, data, room=None, skip_sid=None, namespace=None, |
|
|
def send(self, data, room=None, skip_sid=None, namespace=None, |
|
@ -43,7 +39,7 @@ class Namespace(object): |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.send(data, room=room, skip_sid=skip_sid, |
|
|
return self.server.send(data, room=room, skip_sid=skip_sid, |
|
|
namespace=namespace or self.namespace, |
|
|
namespace=namespace or self.name, |
|
|
callback=callback) |
|
|
callback=callback) |
|
|
|
|
|
|
|
|
def enter_room(self, sid, room, namespace=None): |
|
|
def enter_room(self, sid, room, namespace=None): |
|
@ -54,7 +50,7 @@ class Namespace(object): |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.enter_room(sid, room, |
|
|
return self.server.enter_room(sid, room, |
|
|
namespace=namespace or self.namespace) |
|
|
namespace=namespace or self.name) |
|
|
|
|
|
|
|
|
def leave_room(self, sid, room, namespace=None): |
|
|
def leave_room(self, sid, room, namespace=None): |
|
|
"""Leave a room. |
|
|
"""Leave a room. |
|
@ -64,7 +60,7 @@ class Namespace(object): |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.leave_room(sid, room, |
|
|
return self.server.leave_room(sid, room, |
|
|
namespace=namespace or self.namespace) |
|
|
namespace=namespace or self.name) |
|
|
|
|
|
|
|
|
def close_room(self, room, namespace=None): |
|
|
def close_room(self, room, namespace=None): |
|
|
"""Close a room. |
|
|
"""Close a room. |
|
@ -74,7 +70,7 @@ class Namespace(object): |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.close_room(room, |
|
|
return self.server.close_room(room, |
|
|
namespace=namespace or self.namespace) |
|
|
namespace=namespace or self.name) |
|
|
|
|
|
|
|
|
def rooms(self, sid, namespace=None): |
|
|
def rooms(self, sid, namespace=None): |
|
|
"""Return the rooms a client is in. |
|
|
"""Return the rooms a client is in. |
|
@ -83,7 +79,7 @@ class Namespace(object): |
|
|
that when the ``namespace`` argument is not given the namespace |
|
|
that when the ``namespace`` argument is not given the namespace |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.rooms(sid, namespace=namespace or self.namespace) |
|
|
return self.server.rooms(sid, namespace=namespace or self.name) |
|
|
|
|
|
|
|
|
def disconnect(self, sid, namespace=None): |
|
|
def disconnect(self, sid, namespace=None): |
|
|
"""Disconnect a client. |
|
|
"""Disconnect a client. |
|
@ -93,4 +89,38 @@ class Namespace(object): |
|
|
associated with the class is used. |
|
|
associated with the class is used. |
|
|
""" |
|
|
""" |
|
|
return self.server.disconnect(sid, |
|
|
return self.server.disconnect(sid, |
|
|
namespace=namespace or self.namespace) |
|
|
namespace=namespace or self.name) |
|
|
|
|
|
|
|
|
|
|
|
def get_event_handler(self, event_name): |
|
|
|
|
|
"""Returns the event handler for given ``event`` in this namespace or |
|
|
|
|
|
``None``, if none exists. |
|
|
|
|
|
|
|
|
|
|
|
:param event: The event name the handler is required for. |
|
|
|
|
|
""" |
|
|
|
|
|
for attr_name in dir(self): |
|
|
|
|
|
attr = getattr(self, attr_name) |
|
|
|
|
|
if hasattr(attr, '_sio_event_name'): |
|
|
|
|
|
_event_name = getattr(attr, '_sio_event_name') |
|
|
|
|
|
elif attr_name.startswith('on_'): |
|
|
|
|
|
_event_name = attr_name[3:] |
|
|
|
|
|
else: |
|
|
|
|
|
continue |
|
|
|
|
|
if _event_name == event_name: |
|
|
|
|
|
return attr |
|
|
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
|
def event_name(name): |
|
|
|
|
|
"""Decorator to overwrite event names: |
|
|
|
|
|
|
|
|
|
|
|
@Namespace.event_name("event name with spaces") |
|
|
|
|
|
def foo(self, sid, data): |
|
|
|
|
|
return "received: %s" % data |
|
|
|
|
|
|
|
|
|
|
|
Ensure that you only add well-behaving decorators after this one |
|
|
|
|
|
(meaning such that preserve attributes) because you'll loose them |
|
|
|
|
|
otherwise. |
|
|
|
|
|
""" |
|
|
|
|
|
def wrapper(handler): |
|
|
|
|
|
handler._sio_event_name = name |
|
|
|
|
|
return handler |
|
|
|
|
|
return wrapper |
|
|