我正在开发一个运行良好的 Flask-SocketIO 服务器。
但是,我在服务器日志中收到很多这样的请求:
"GET /socket.io/?EIO=3&transport=polling&t=LBS1TQt HTTP/1.1"
这是我正在使用的代码:
from flask import Flask, render_template, redirect, url_for
from flask_socketio import SocketIO, emit
import json
def load_config():
# configuration
return json.load(open('/etc/geekdj/config.json'))
config = load_config()
geekdj = Flask(__name__)
geekdj.config["DEBUG"] = config["debug"]
geekdj.config["SECRET_KEY"] = config["secret_key"]
geekdj.config.from_envvar("FLASKR_SETTINGS", silent=True)
socketio = SocketIO(geekdj)
@geekdj.route('/')
def index():
return render_template('index.html')
# SocketIO functions
@socketio.on('connect')
def chat_connect():
print ('connected')
@socketio.on('disconnect')
def chat_disconnect():
print ("Client disconnected")
@socketio.on('broadcast')
def chat_broadcast(message):
print ("test")
emit("chat", {'data': message['data']})
if __name__ == "__main__":
socketio.run(geekdj, port=8000)
和 JS index.html
:
<script src="//cdn.socket.io/socket.io-1.4.5.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// the socket.io documentation recommends sending an explicit package upon connection
// this is specially important when using the global namespace
var socket = io.connect('http://localhost:8000');
socket.on('connection', function(socket) {
socket.emit('foo', {foo: "bar"});
socket.join("test");
});
socket.on('joined', function(data) {
console.log('Joined room!');
console.log(data["room"]);
});
});
如果可能的话,我宁愿使用实际的 Websockets,有谁知道为什么 SocketIO 会退回到轮询?