def __init__(self, settings=None): """ Initialise the SockJS Endpoint. :param settings: If supplied, must be a dict of settings to override :ref:`DEFAULT_SETTINGS`. """ self.active_sessions = {} self.started = False self.settings = DEFAULT_SETTINGS.copy() if settings: self.settings.update(settings) self.session_pool = self.session_pool_class( self.settings['session_check_interval'], self.settings['heartbeat_delay'], ) self.stats = stats.StatsCollector() self.start()
def __init__(self, connection, prefix='', user_settings=dict(), io_loop=None, session_kls=None): """Constructor. `connection` SockJSConnection class `prefix` Connection prefix `user_settings` Settings dictionary `io_loop` Optional IOLoop instance """ # TODO: Version check if version_info[0] < 2: raise Exception('sockjs-tornado requires Tornado 2.0 or higher.') # Store connection class self._connection = connection # Initialize io_loop self.io_loop = io_loop or ioloop.IOLoop.instance() # Settings self.settings = DEFAULT_SETTINGS.copy() if user_settings: self.settings.update(user_settings) self.websockets_enabled = 'websocket' not in self.settings['disabled_transports'] self.cookie_needed = self.settings['jsessionid'] # Sessions self._session_kls = session_kls if session_kls else session.Session self._sessions = sessioncontainer.SessionContainer() check_interval = self.settings['session_check_interval'] * 1000 self._sessions_cleanup = ioloop.PeriodicCallback(self._sessions.expire, check_interval) self._sessions_cleanup.start() # Stats self.stats = stats.StatsCollector(self.io_loop) # Initialize URLs base = prefix + r'/[^/.]+/(?P<session_id>[^/.]+)' # Generate global handler URLs self._transport_urls = [('%s/%s$' % (base, p[0]), p[1], dict(server=self)) for p in GLOBAL_HANDLERS] for k, v in TRANSPORTS.items(): if k in self.settings['disabled_transports']: continue # Only version 1 is supported self._transport_urls.append( (r'%s/%s$' % (base, k), v, dict(server=self)) ) # Generate static URLs self._transport_urls.extend([('%s%s' % (prefix, k), v, dict(server=self)) for k, v in STATIC_HANDLERS.items()])