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

vge*_*gel 9 python websocket flask socket.io web

我正在使用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
Run Code Online (Sandbox Code Playgroud)

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

#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()
Run Code Online (Sandbox Code Playgroud)

我的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()
Run Code Online (Sandbox Code Playgroud)

但是,我遇到的问题是客户端没有触发'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>])
Run Code Online (Sandbox Code Playgroud)

所以没什么特别的.

编辑:客户端代码:

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) {
    ...
})
Run Code Online (Sandbox Code Playgroud)

小智 1

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

dapp = werkzeug.debug.DebuggedApplication(app, evalex = True)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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