gevent-socketio没有使用我的@ app.route端点进行socketio

时间:2013-08-19 17:11:12

标签: python web websocket socket.io flask

我正在使用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

我正在使用非常标准的设置来启动服务器:

#Called from __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()

我的SocketIO命名空间的一个非常标准的钩子:

@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 as e:
        app.logger.error("Exception while handling socketio connection", exc_info=True)
    return flask.Response()

但是,我遇到的问题是客户端没有触发'connect'事件。经过一番挖掘,我意识到即使我在输出中收到127.0.0.1 - - [2013-08-19 12:53:57] "GET /socket.io/1/websocket/170191232666 HTTP/1.1" 101 - -条消息,我也没有收到ws connect消息(而代码中的其他打印语句工作正常)。我注释掉了这个端点,果然它甚至没有被调用。这可以解释为什么我的命名空间没有被使用。但为什么?我是否错误地注册了我的命名空间?

print app.url_map收益:

Map([<Rule '/' (HEAD, OPTIONS, GET) -> root>,
 <Rule '/socket.io/<rest>' (HEAD, OPTIONS, GET) -> push_stream>,
 <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])

没有什么不同寻常的。

编辑:客户端代码:

socket = io.connect('/join_notification')
console.log(socket)

socket.on('connect', function() {
    console.log('connected to websocket')
    socket.emit('login', {'name': data['name']})
})

socket.on('disconnect', function() {
    console.log('d/c\'d from websocket')
})

socket.on('join_error', function() {
    ...
})

socket.on('join_success', function(data){
    ...
})

socket.on('join', function(data) {
    ...
})

2 个答案:

答案 0 :(得分:1)

奇怪的行为是因为这一行:

dapp = werkzeug.debug.DebuggedApplication(app, evalex = True)

Socketio和werkzeug调试器无法一起工作。关于此问题已经存在一个未解决的问题:https://github.com/abourget/gevent-socketio/issues/114

但您可以通过创建自定义调试器类来解决它。

from werkzeug.debug import DebuggedApplication
class MyDebuggedApplication(DebuggedApplication):
    def __call__(self, environ, start_response):
        # check if websocket call
        if "wsgi.websocket" in environ and not environ["wsgi.websocket"] is None:
            # a websocket call, no debugger ;)
            return self.app(environ, start_response)
        # else go on with debugger
        return DebuggedApplication.__call__(self, environ, start_response)

# remember to call the overwritten debugger in your run_dev_server() function
dapp = MyDebuggedApplication(app, evalex = True)

补丁依赖于环境密钥wsgi.websocket,它似乎只出现在websocket调用中。小心,我没有多想,可能还有其他问题。

答案 1 :(得分:1)

我花了一段时间,但看起来我已经解决了它,享受:

https://github.com/Aldanor/SocketIO-Flask-Debug

简而言之:

  • 每当发现socket.io请求时,您需要显式地从werkzeug.debug.DebuggedApplication返回的生成器中提取值,这样才能正确建立套接字连接
  • 默认情况下,
  • socket.io名称空间处理程序不会被werkzeug覆盖,因此您需要插入自己的try catch,如果发现异常则保存回溯,然后在请求上下文中的某处重新加载
相关问题