Browse Source

add server generate event in example

pull/31/head
sillygod 9 years ago
parent
commit
bb3beb7af2
  1. 12
      examples/django_chat/chat/sockets.py
  2. 5
      examples/django_chat/sdjango/__init__.py
  3. 6
      examples/django_chat/sdjango/sd_manager.py

12
examples/django_chat/chat/sockets.py

@ -5,6 +5,13 @@ from sdjango import namespace
online_user_num = 0
def background_thread():
while True:
namespace.server.sleep(10)
namespace.server.emit("my response", {'data': 'Server generated event'}, namespace='/test')
@namespace('/test')
class TestNamespace:
@ -12,6 +19,11 @@ class TestNamespace:
self.name = name
self.request = None # django request object
def initialize(self):
"""the server is already assigned when this function is called.
"""
namespace.server.start_background_task(background_thread)
def _get_socket(self, sid):
socket = namespace.server.eio._get_socket(sid)
return socket

5
examples/django_chat/sdjango/__init__.py

@ -71,6 +71,8 @@ class namespace:
methods = inspect.getmembers(instance, predicate=inspect.ismethod)
namespace.handler_container[self.name].append(instance)
for key, value in methods:
if key.startswith('on_'):
namespace.handler_container[self.name].append(value)
@ -85,6 +87,9 @@ class namespace:
for name, handlers in namespace.handler_container.items():
instance = handlers.pop(0)
instance.initialize()
for obj in handlers:
event_name = obj.__name__.replace('on_', '').replace('_', ' ')
server.on(event_name, obj, name)

6
examples/django_chat/sdjango/sd_manager.py

@ -3,20 +3,16 @@ from socketio.base_manager import BaseManager
class SdManager(BaseManager):
"""
"""Currently, there is no need to use custom manager
"""
def initialize(self, server):
# import pdb; pdb.set_trace()
super().initialize(server)
def connect(self, sid, namespace):
"""Register a client connection to a namespace.
and set the django request object?
"""
# TODO: process user authentication here?
# if 'django_request' in self.server.environ[sid]:
# print(self.server.environ[sid]['django_request'].user)
self.enter_room(sid, namespace, None)
self.enter_room(sid, namespace, sid)

Loading…
Cancel
Save