python - gevent-socketio not using my @app.route endpoint for socketio -
i using flask gevent-socketio:
$ cat requirements.txt flask==0.10.1 jinja2==2.7.1 markupsafe==0.18 werkzeug==0.9.3 argparse==1.2.1 gevent==0.13.8 gevent-socketio==0.3.5-rc2 gevent-websocket==0.3.6 greenlet==0.4.1 itsdangerous==0.23 wsgiref==0.1.2
i'm using pretty standard setup start server:
#called __main__ def run_dev_server(): app.debug = true port = 5000 dapp = werkzeug.debug.debuggedapplication(app, evalex = true) socketioserver(('', port), dapp, resource="socket.io").serve_forever()
and pretty standard hook socketio namespace:
@app.route('/socket.io/<path:rest>') def push_stream(rest): print 'ws connect', rest try: socketio.socketio_manage(request.environ, {'/join_notification': joinsnamespace}, request) except exception e: app.logger.error("exception while handling socketio connection", exc_info=true) return flask.response()
however, i'm having problems 'connect' events aren't being fired on client. after little digging, realized though getting 127.0.0.1 - - [2013-08-19 12:53:57] "get /socket.io/1/websocket/170191232666 http/1.1" 101 - -
messages in output, wasn't getting ws connect
message (while other print statements in code working fine). commented out endpoint, , sure enough it's not being called. explain why namespace isn't being used. why? registering namespace wrong?
print app.url_map
yields:
map([<rule '/' (head, options, get) -> root>, <rule '/socket.io/<rest>' (head, options, get) -> push_stream>, <rule '/static/<filename>' (head, options, get) -> static>])
so nothing out of ordinary.
edit: client code:
socket = io.connect('/join_notification') console.log(socket) socket.on('connect', function() { console.log('connected websocket') socket.emit('login', {'name': data['name']}) }) socket.on('disconnect', function() { console.log('d/c\'d websocket') }) socket.on('join_error', function() { ... }) socket.on('join_success', function(data){ ... }) socket.on('join', function(data) { ... })
the weird behavior because of line:
dapp = werkzeug.debug.debuggedapplication(app, evalex = true)
socketio , werkzeug debugger don't work together. there open issue see: https://github.com/abourget/gevent-socketio/issues/114
but can work around making custom debugger class.
from werkzeug.debug import debuggedapplication class mydebuggedapplication(debuggedapplication): def __call__(self, environ, start_response): # check if websocket call if "wsgi.websocket" in environ , not environ["wsgi.websocket"] none: # websocket call, no debugger ;) return self.app(environ, start_response) # else go on debugger return debuggedapplication.__call__(self, environ, start_response) # remember call overwritten debugger in run_dev_server() function dapp = mydebuggedapplication(app, evalex = true)
the patch relies on environment-key wsgi.websocket, seems present in websocket calls. careful, haven't put thought that, there might other issues.
Comments
Post a Comment