我用的是酒瓶和:
$ 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()但是,当“连接”事件没有被激发到客户端时,我遇到了问题。经过一番研究后,我意识到,即使我在输出中获得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) {
...
})发布于 2013-08-31 19:59:06
奇怪的行为是因为这句话:
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调用中。小心点,我没想太多,可能还有其他的问题。
发布于 2013-09-29 14:44:18
花了我一段时间,但看起来我已经解决了,好好享受吧:
https://github.com/Aldanor/SocketIO-Flask-Debug
简而言之:
https://stackoverflow.com/questions/18319345
复制相似问题