这是一个简单的flask-socketio服务器:
from flask import Flask, render_template, request
from flask.ext.socketio import SocketIO, send, emit, join_room, leave_room
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('connect')
def connect():
print('connected')
emit('response', {'status':'ok'})
if __name__ == '__main__':
socketio.run(app)
如此连接:
<script type="text/javascript" src="../static/socket.io.min.js"></script>
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('connect', function() {
console.log('connected');
socket.emit('connect');
});
socket.on('response', function(data){
console.log(data);
});
</script>
所以当我打开我在浏览器控制台中看到的页面时:
(index):6 connected
(index):10 Object {status: "ok"}
但是当我更改前端的资源时:
var socket = io.connect('http://' + document.domain + ':' + location.port, {resource:'resource2'});
并在后端:
if __name__ == '__main__':
socketio.run(app, resource='resource2')
我只看到这个
(index):6 connected
所以什么都没发出。此外,后端不会调用print('connected')
。
资源覆盖有什么问题?