gpt4 book ai didi

python - 使用轮询而不是 websockets 的 Flask-SocketIO 服务器

转载 作者:太空狗 更新时间:2023-10-30 01:57:53 25 4
gpt4 key购买 nike

我正在开发一个工作正常的 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)

index.html 中的 JS:

<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 会退回到轮询吗?

最佳答案

第一个答案有效吗?如果是这样,你应该接受它。如果没有,请发布您的 requirements.txt。

我遇到了同样的问题,通过充分吸收文档页面找到了解决方案:

The asynchronous services that this package relies on can be selected among three choices:

  • eventlet is the best performant option, with support for long-polling and WebSocket transports.
  • gevent is supported in a number of different configurations. The long-polling transport is fully supported with the gevent package,
    but unlike eventlet, gevent does not have native WebSocket support.
    To add support for WebSocket there are currently two options.
    Installing the gevent-websocket package adds WebSocket support to
    gevent or one can use the uWSGI web server, which comes with
    WebSocket functionality. The use of gevent is also a performant
    option, but slightly lower than eventlet.
  • The Flask development server based on Werkzeug can be used as well, with the caveat that it lacks the performance of the other two
    options, so it should only be used to simplify the development
    workflow. This option only supports the long-polling transport.

基本上,我的虚拟环境中没有 evenlet 或 gevent-websocket。我安装了 eventlet,传输升级到 websocket 几乎是瞬间完成的!希望这会有所帮助。

关于python - 使用轮询而不是 websockets 的 Flask-SocketIO 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com