Merge branch 'wuurrd-master'
This commit is contained in:
commit
6661d8be0e
2 changed files with 35 additions and 22 deletions
|
|
@ -32,6 +32,9 @@ class BaseNamespace(object):
|
|||
self._callback_by_event = {}
|
||||
self.initialize()
|
||||
|
||||
def _log(self, level, msg, *attrs):
|
||||
_log.log(level, '%s: %s' % (self._transport._url, msg), *attrs)
|
||||
|
||||
def initialize(self):
|
||||
'Initialize custom variables here; you can override this method'
|
||||
pass
|
||||
|
|
@ -52,19 +55,19 @@ class BaseNamespace(object):
|
|||
|
||||
def on_connect(self):
|
||||
'Called after server connects; you can override this method'
|
||||
_log.debug('%s [connect]', self.path)
|
||||
self._log(logging.DEBUG, '%s [connect]', self.path)
|
||||
|
||||
def on_disconnect(self):
|
||||
'Called after server disconnects; you can override this method'
|
||||
_log.debug('%s [disconnect]', self.path)
|
||||
self._log(logging.DEBUG, '%s [disconnect]', self.path)
|
||||
|
||||
def on_heartbeat(self):
|
||||
'Called after server sends a heartbeat; you can override this method'
|
||||
_log.debug('%s [heartbeat]', self.path)
|
||||
self._log(logging.DEBUG, '%s [heartbeat]', self.path)
|
||||
|
||||
def on_message(self, data):
|
||||
'Called after server sends a message; you can override this method'
|
||||
_log.info('%s [message] %s', self.path, data)
|
||||
self._log(logging.INFO, '%s [message] %s', self.path, data)
|
||||
|
||||
def on_event(self, event, *args):
|
||||
"""
|
||||
|
|
@ -77,27 +80,28 @@ class BaseNamespace(object):
|
|||
if callback:
|
||||
arguments.append('callback(*args)')
|
||||
callback(*args)
|
||||
_log.info('%s [event] %s(%s)', self.path, event, ', '.join(arguments))
|
||||
self._log(logging.INFO, '%s [event] %s(%s)', self.path, event,
|
||||
', '.join(arguments))
|
||||
|
||||
def on_error(self, reason, advice):
|
||||
'Called after server sends an error; you can override this method'
|
||||
_log.info('%s [error] %s', self.path, advice)
|
||||
self._log(logging.INFO, '%s [error] %s', self.path, advice)
|
||||
|
||||
def on_noop(self):
|
||||
'Called after server sends a noop; you can override this method'
|
||||
_log.info('%s [noop]', self.path)
|
||||
self._log(logging.INFO, '%s [noop]', self.path)
|
||||
|
||||
def on_open(self, *args):
|
||||
_log.info('%s [open] %s', self.path, args)
|
||||
self._log(logging.INFO, '%s [open] %s', self.path, args)
|
||||
|
||||
def on_close(self, *args):
|
||||
_log.info('%s [close] %s', self.path, args)
|
||||
self._log(logging.INFO, '%s [close] %s', self.path, args)
|
||||
|
||||
def on_retry(self, *args):
|
||||
_log.info('%s [retry] %s', self.path, args)
|
||||
self._log(logging.INFO, '%s [retry] %s', self.path, args)
|
||||
|
||||
def on_reconnect(self, *args):
|
||||
_log.info('%s [reconnect] %s', self.path, args)
|
||||
self._log(logging.INFO, '%s [reconnect] %s', self.path, args)
|
||||
|
||||
def _find_event_callback(self, event):
|
||||
# Check callbacks defined by on()
|
||||
|
|
@ -141,6 +145,10 @@ class SocketIO(object):
|
|||
self.kw = kw
|
||||
self.define(Namespace)
|
||||
|
||||
def log(self, level, msg, *attrs):
|
||||
_log.log(level, '%s: %s' % (self.base_url, msg),
|
||||
*attrs)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
|
|
@ -188,7 +196,7 @@ class SocketIO(object):
|
|||
warning = Exception('[connection error] %s' % e)
|
||||
warning_screen.throw(warning)
|
||||
except StopIteration:
|
||||
_log.warn(warning)
|
||||
self.log(logging.WARNING, warning)
|
||||
self.disconnect()
|
||||
|
||||
def _process_events(self):
|
||||
|
|
@ -196,7 +204,7 @@ class SocketIO(object):
|
|||
try:
|
||||
self._process_packet(packet)
|
||||
except PacketError as e:
|
||||
_log.warn('[packet error] %s', e)
|
||||
self.log(logging.WARNING, '[packet error] %s', e)
|
||||
|
||||
def _process_packet(self, packet):
|
||||
code, packet_id, path, data = packet
|
||||
|
|
@ -246,13 +254,13 @@ class SocketIO(object):
|
|||
warning = Exception('[waiting for connection] %s' % e)
|
||||
warning_screen.throw(warning)
|
||||
except StopIteration:
|
||||
_log.warn(warning)
|
||||
self.log(logging.WARNING, warning)
|
||||
return self.__transport
|
||||
|
||||
def _get_transport(self):
|
||||
socketIO_session = _get_socketIO_session(
|
||||
self.is_secure, self.base_url, **self.kw)
|
||||
_log.debug('[transports available] %s', ' '.join(
|
||||
self.log(logging.DEBUG, '[transports available] %s', ' '.join(
|
||||
socketIO_session.server_supported_transports))
|
||||
# Initialize heartbeat_pacemaker
|
||||
self.heartbeat_pacemaker = self._make_heartbeat_pacemaker(
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ class _AbstractTransport(object):
|
|||
self._wants_to_disconnect = False
|
||||
self._packets = []
|
||||
|
||||
def _log(self, level, msg, *attrs):
|
||||
_log.log(level, '[%s] %s' % (self._url, msg), *attrs)
|
||||
|
||||
def disconnect(self, path=''):
|
||||
if not path:
|
||||
self._wants_to_disconnect = True
|
||||
|
|
@ -68,7 +71,7 @@ class _AbstractTransport(object):
|
|||
packet_parts = str(code), packet_id, path, data
|
||||
packet_text = ':'.join(packet_parts)
|
||||
self.send(packet_text)
|
||||
_log.debug('[packet sent] %s', packet_text)
|
||||
self._log(logging.DEBUG, '[packet sent] %s', packet_text)
|
||||
|
||||
def recv_packet(self):
|
||||
try:
|
||||
|
|
@ -77,11 +80,11 @@ class _AbstractTransport(object):
|
|||
except IndexError:
|
||||
pass
|
||||
for packet_text in self.recv():
|
||||
_log.debug('[packet received] %s', packet_text)
|
||||
self._log(logging.DEBUG, '[packet received] %s', packet_text)
|
||||
try:
|
||||
packet_parts = packet_text.split(':', 3)
|
||||
except AttributeError:
|
||||
_log.warn('[packet error] %s', packet_text)
|
||||
self._log(logging.WARNING, '[packet error] %s', packet_text)
|
||||
continue
|
||||
code, packet_id, path, data = None, None, None, None
|
||||
packet_count = len(packet_parts)
|
||||
|
|
@ -120,6 +123,7 @@ class _WebsocketTransport(_AbstractTransport):
|
|||
url = '%s://%s/websocket/%s' % (
|
||||
'wss' if is_secure else 'ws',
|
||||
base_url, socketIO_session.id)
|
||||
self._url = url
|
||||
try:
|
||||
self._connection = websocket.create_connection(url)
|
||||
except socket.timeout as e:
|
||||
|
|
@ -137,11 +141,11 @@ class _WebsocketTransport(_AbstractTransport):
|
|||
self._connection.send(packet_text)
|
||||
except websocket.WebSocketTimeoutException as e:
|
||||
message = 'timed out while sending %s (%s)' % (packet_text, e)
|
||||
_log.warn(message)
|
||||
self._log(logging.WARNING, message)
|
||||
raise TimeoutError(e)
|
||||
except socket.error as e:
|
||||
message = 'disconnected while sending %s (%s)' % (packet_text, e)
|
||||
_log.warn(message)
|
||||
self._log(logging.WARNING, message)
|
||||
raise ConnectionError(message)
|
||||
|
||||
def recv(self):
|
||||
|
|
@ -259,7 +263,7 @@ class _JSONP_PollingTransport(_AbstractTransport):
|
|||
self._id, response_text = self.RESPONSE_PATTERN.match(
|
||||
response_text).groups()
|
||||
except AttributeError:
|
||||
_log.warn('[packet error] %s', response_text)
|
||||
self._log(logging.WARNING, '[packet error] %s', response_text)
|
||||
return
|
||||
if not response_text.startswith(BOUNDARY):
|
||||
yield response_text.decode('unicode_escape')
|
||||
|
|
@ -282,7 +286,8 @@ def _negotiate_transport(
|
|||
server_supported_transports = session.server_supported_transports
|
||||
for supported_transport in client_supported_transports:
|
||||
if supported_transport in server_supported_transports:
|
||||
_log.debug('[transport selected] %s', supported_transport)
|
||||
_log.debug('[%s] [transport selected] %s', base_url,
|
||||
supported_transport)
|
||||
return {
|
||||
'websocket': _WebsocketTransport,
|
||||
'xhr-polling': _XHR_PollingTransport,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue