Search code examples
turbogears2keyerrorgevent-socketio

Turbogears with gevent-socketio: request Key Error


I try to use gevent.socketio with my TurboGears 2 Website:

in the ini-file i use

[server:main]
use = egg:gevent-socketio#paster
transports = xhr-multipart, xhr-polling, websocket
host = 0.0.0.0
port = 8080

when i try to access the controller in the Webbrowser:

@expose('wago.templates.test')
def index(self):
  socketio_manage(request.environ, {"/stat": StatNamespace}, request=request) 
  return dict()

i get the following error:

Traceback (most recent call last):
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/wsgiapp.py", line 105, in __call__
response = self.wrapped_dispatch(controller, environ, context)
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/wsgiapp.py", line 278, in dispatch
return controller(environ, context)
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/controllers/dispatcher.py", line 132, in __call__
response = self._perform_call(context)
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/controllers/dispatcher.py", line 113, in _perform_call
r = self._call(func, params, remainder=remainder, context=context)
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/controllers/decoratedcontroller.py", line 120, in _call
output = controller_caller(context_config, bound_controller_callable, remainder, params)
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/decorators.py", line 42, in _decorated_controller_caller
return application_controller_caller(tg_config, controller, remainder, params)
File "/home/pi/tgenv/lib/python2.7/site-packages/tg/configuration/app_config.py", line 124, in call_controller
return controller(*remainder, **params)
File "/home/pi/tgenv/WAGO/wago/controllers/root.py", line 13, in index
socketio_manage(request.environ, {"/stat": StatNamespace}, request=request)
File "/home/pi/tgenv/lib/python2.7/site-packages/socketio/__init__.py", line 67, in socketio_manage
socket = environ['socketio']
KeyError: 'socketio'

I used several tutorials for pyramid to introduce myself to gevent-socketio. I tried it with older versions from TurboGears2, gevent and gevent-socketio, i also tried this module but always the same error. i'm pretty new to sockets, so maybe i'm just missing something obvious


Solution

  • the gevent-socketio recognizes socket requests only from a specific url (socket.io/1/) because TurboGears uses the python function names as url it is not posible to us "." or "1" on the regular way. A simple solution:

        @expose()
        def _default(self, *args):
          args = list(args)
          if "socketio" in request.environ:
            #do socketio stuff...
          else:
            abort(404)