如何正确安装flask-socketIO? [英] How do i properly install flask-socketIO?

查看:95
本文介绍了如何正确安装flask-socketIO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac上安装了多次Flask-socketio,请仔细阅读说明并安装要求(eventlet/gevent).尽管当我运行简单的代码进行测试时,它表示我尚未导入模块,或者什么也没有显示,直到我在浏览器中打开index.html,然后显示以下内容:客户端使用的是不支持的Socket.IO或Engine.IO协议版本(此错误的进一步发生将以INFO级别记录)

I have installed multiple times Flask-socketio on my mac, closely reading the instructions and installing the requirements (eventlet/gevent). Athough when i run my simple code to test, it either says that i have not imported the modules or show nothing until i open index.html in my browser where it then displays : The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)

这是我的app.py代码:

Here is my app.py code:

from flask import Flask
from flask_socketio import SocketIO, send

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hello'
    
socketio = SocketIO(app, cors_allowed_origins='*')
@socketio.on('message')
def handle(msg):
    print("message: "+msg)
    send(msg, bradcast=True)

if __name__ == '__main__':
    socketio.run(app)

这是我的终端窗口:

这是我的index.html代码(如果需要):

Here is my index.html code (if needed):

<html>
<head>
<title>Chat Room</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {

    var socket = io.connect('http://127.0.0.1:5000');

    socket.on('connect', function() {
        socket.send('User has connected!');
    });

    socket.on('message', function(msg) {
        $("#messages").append('<li>'+msg+'</li>');
        console.log('Received message');
    });

    $('#sendbutton').on('click', function() {
        socket.send($('#myMessage').val());
        $('#myMessage').val('');
    });

});
</script>
<ul id="messages"></ul>
<input type="text" id="myMessage">
<button id="sendbutton">Send</button>
</body>
</html>

谢谢您的帮助

推荐答案

检查烧瓶-SocketIO文档以获取有关版本兼容性的信息.

Check the Flask-SocketIO docs for information about version compatibility.

您已经安装了Flask-SocketIO版本5,因此需要JavaScript客户端版本3,但是您具有1.4.8.

You have installed Flask-SocketIO version 5, so you need version 3 of the JavaScript client, but you have 1.4.8.

使用此CDN URL代替版本3.0.5: https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.5/socket.io.min.js

Use this CDN URL instead for version 3.0.5: https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.5/socket.io.min.js

这篇关于如何正确安装flask-socketIO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆