Split into modules
This commit is contained in:
parent
9afe5c2f2a
commit
5ecc5fb36c
7 changed files with 625 additions and 510 deletions
198
socketIO_client/namespaces.py
Normal file
198
socketIO_client/namespaces.py
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
from .logs import LoggingMixin
|
||||
|
||||
|
||||
class EngineIONamespace(LoggingMixin):
|
||||
'Define engine.io client behavior'
|
||||
|
||||
def __init__(self, io):
|
||||
self._io = io
|
||||
self._callback_by_event = {}
|
||||
self._log_name = io._url
|
||||
self.initialize()
|
||||
|
||||
def initialize(self):
|
||||
"""Initialize custom variables here.
|
||||
You can override this method."""
|
||||
|
||||
def on(self, event, callback):
|
||||
'Define a callback to handle an event emitted by the server'
|
||||
self._callback_by_event[event] = callback
|
||||
|
||||
def on_open(self):
|
||||
"""Called after engine.io connects.
|
||||
You can override this method."""
|
||||
|
||||
def on_close(self):
|
||||
"""Called after engine.io disconnects.
|
||||
You can override this method."""
|
||||
|
||||
def on_ping(self, data):
|
||||
"""Called after engine.io sends a ping packet.
|
||||
You can override this method."""
|
||||
|
||||
def on_pong(self, data):
|
||||
"""Called after engine.io sends a pong packet.
|
||||
You can override this method."""
|
||||
|
||||
def on_message(self, data):
|
||||
"""Called after engine.io sends a message packet.
|
||||
You can override this method."""
|
||||
|
||||
def on_upgrade(self):
|
||||
"""Called after engine.io sends an upgrade packet.
|
||||
You can override this method."""
|
||||
|
||||
def on_noop(self):
|
||||
"""Called after engine.io sends a noop packet.
|
||||
You can override this method."""
|
||||
|
||||
def _find_packet_callback(self, event):
|
||||
# Check callbacks defined by on()
|
||||
try:
|
||||
return self._callback_by_event[event]
|
||||
except KeyError:
|
||||
pass
|
||||
# Check callbacks defined explicitly
|
||||
return getattr(self, 'on_' + event)
|
||||
|
||||
|
||||
class SocketIONamespace(EngineIONamespace):
|
||||
'Define socket.io client behavior'
|
||||
|
||||
def __init__(self, io, path):
|
||||
self.path = path
|
||||
super(SocketIONamespace, self).__init__(io)
|
||||
|
||||
def on_connect(self):
|
||||
"""Called after socket.io connects.
|
||||
You can override this method."""
|
||||
|
||||
def on_reconnect(self):
|
||||
"""Called after socket.io reconnects.
|
||||
You can override this method."""
|
||||
|
||||
def on_disconnect(self):
|
||||
"""Called after socket.io disconnects.
|
||||
You can override this method."""
|
||||
|
||||
def on_event(self, event, *args):
|
||||
"""
|
||||
Called if there is no matching event handler.
|
||||
You can override this method.
|
||||
There are three ways to define an event handler:
|
||||
|
||||
- Call socketIO.on()
|
||||
|
||||
socketIO = SocketIO('localhost', 8000)
|
||||
socketIO.on('my_event', my_function)
|
||||
|
||||
- Call namespace.on()
|
||||
|
||||
namespace = socketIO.get_namespace()
|
||||
namespace.on('my_event', my_function)
|
||||
|
||||
- Define namespace.on_xxx
|
||||
|
||||
class Namespace(SocketIONamespace):
|
||||
|
||||
def on_my_event(self, *args):
|
||||
my_function(*args)
|
||||
|
||||
socketIO.define(Namespace)"""
|
||||
|
||||
def on_error(self, data):
|
||||
"""Called after socket.io sends an error packet.
|
||||
You can override this method."""
|
||||
|
||||
def _find_packet_callback(self, event):
|
||||
# Interpret events
|
||||
if event == 'connect':
|
||||
if not hasattr(self, '_was_connected'):
|
||||
self._was_connected = True
|
||||
else:
|
||||
event = 'reconnect'
|
||||
# Check callbacks defined by on()
|
||||
try:
|
||||
return self._callback_by_event[event]
|
||||
except KeyError:
|
||||
pass
|
||||
# Check callbacks defined explicitly or use on_event()
|
||||
return getattr(
|
||||
self, 'on_' + event.replace(' ', '_'),
|
||||
lambda *args: self.on_event(event, *args))
|
||||
|
||||
|
||||
class LoggingEngineIONamespace(EngineIONamespace):
|
||||
|
||||
def on_open(self):
|
||||
self._debug('[open]')
|
||||
super(LoggingEngineIONamespace, self).on_open()
|
||||
|
||||
def on_close(self):
|
||||
self._debug('[close]')
|
||||
super(LoggingEngineIONamespace, self).on_close()
|
||||
|
||||
def on_ping(self, data):
|
||||
self._debug('[ping] %s' % data)
|
||||
super(LoggingEngineIONamespace, self).on_ping(data)
|
||||
|
||||
def on_pong(self, data):
|
||||
self._debug('[pong] %s' % data)
|
||||
super(LoggingEngineIONamespace, self).on_pong(data)
|
||||
|
||||
def on_message(self, data):
|
||||
self._debug('[message] %s' % data)
|
||||
super(LoggingEngineIONamespace, self).on_message(data)
|
||||
|
||||
def on_upgrade(self):
|
||||
self._debug('[upgrade]')
|
||||
super(LoggingEngineIONamespace, self).on_upgrade()
|
||||
|
||||
def on_noop(self):
|
||||
self._debug('[noop]')
|
||||
super(LoggingEngineIONamespace, self).on_noop()
|
||||
|
||||
def on_event(self, event, *args):
|
||||
callback, args = find_callback(args)
|
||||
arguments = [repr(_) for _ in args]
|
||||
if callback:
|
||||
arguments.append('callback(*args)')
|
||||
self._info('[event] %s(%s)', event, ', '.join(arguments))
|
||||
super(LoggingEngineIONamespace, self).on_event(event, *args)
|
||||
|
||||
|
||||
class LoggingSocketIONamespace(SocketIONamespace):
|
||||
|
||||
def on_connect(self):
|
||||
self._debug('%s [connect]' % self.path)
|
||||
super(LoggingSocketIONamespace, self).on_connect()
|
||||
|
||||
def on_reconnect(self):
|
||||
self._debug('%s [reconnect]' % self.path)
|
||||
super(LoggingSocketIONamespace, self).on_reconnect()
|
||||
|
||||
def on_disconnect(self):
|
||||
self._debug('%s [disconnect]' % self.path)
|
||||
super(LoggingSocketIONamespace, self).on_disconnect()
|
||||
|
||||
def on_event(self, event, *args):
|
||||
callback, args = find_callback(args)
|
||||
arguments = [repr(_) for _ in args]
|
||||
if callback:
|
||||
arguments.append('callback(*args)')
|
||||
self._info('%s [event] %s(%s)', self.path, event, ', '.join(arguments))
|
||||
super(LoggingSocketIONamespace, self).on_event(event, *args)
|
||||
|
||||
def on_error(self, data):
|
||||
self._debug('%s [error] %s' % (self.path, data))
|
||||
super(LoggingSocketIONamespace, self).on_error()
|
||||
|
||||
|
||||
def find_callback(args, kw=None):
|
||||
'Return callback whether passed as a last argument or as a keyword'
|
||||
if args and callable(args[-1]):
|
||||
return args[-1], args[:-1]
|
||||
try:
|
||||
return kw['callback'], args
|
||||
except (KeyError, TypeError):
|
||||
return None, args
|
||||
Loading…
Add table
Add a link
Reference in a new issue