Esempio n. 1
0
    def __init__(self,
                 connection,
                 user_settings=dict(),
                 namespace='socket.io',
                 io_loop=None):
        """Constructor.

        `connection`
            SocketConnection class instance
        `user_settings`
            Settings
        `namespace`
            Router namespace, defaulted to 'socket.io'
        `io_loop`
            IOLoop instance, optional.
        """

        # TODO: Version check
        if version_info[0] < 2:
            raise Exception('TornadIO2 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)

        # Sessions
        self._sessions = sessioncontainer.SessionContainer()

        check_interval = self.settings['session_check_interval']
        self._sessions_cleanup = ioloop.PeriodicCallback(
            self._sessions.expire, check_interval, self.io_loop)
        self._sessions_cleanup.start()

        # Stats
        self.stats = stats.StatsCollector()
        self.stats.start(self.io_loop)

        # Initialize URLs
        self._transport_urls = [(r'/%s/(?P<version>\d+)/$' % namespace,
                                 HandshakeHandler, dict(server=self))]

        for t in self.settings.get('enabled_protocols', dict()):
            proto = PROTOCOLS.get(t)

            if not proto:
                # TODO: Error logging
                continue

            # Only version 1 is supported
            self._transport_urls.append(
                (r'/%s/1/%s/(?P<session_id>[^/]+)/?' % (namespace, t), proto,
                 dict(server=self)))
Esempio n. 2
0
 def __init__(self, conn):
     self._connection = conn
     self.settings = dict(
             session_check_interval=15,
             session_expiry=30,
             heartbeat_interval=12,
             enabled_protocols=['websocket', 'flashsocket', 'xhr-polling',
                                'jsonp-polling', 'htmlfile'],
             xhr_polling_timeout=20
     )
     self.stats = stats.StatsCollector()