1

这是一个简单的 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').

资源覆盖有什么问题?

4

0 回答 0