简体   繁体   English

未捕获的引用错误:flask 中未定义 io

[英]Uncaught ReferenceError: io is not defined in flask

I'm trying to make a chat application with flask and socketio but I get an Uncaught ReferenceError: io is not defined error in my web browsers inspector.我正在尝试使用 flask 和 socketio 创建一个聊天应用程序,但我得到一个 Uncaught ReferenceError: io is not defined 错误在我的 web 浏览器检查器中。 Googling this error didn't give me much.谷歌搜索这个错误并没有给我太多。

Here is my python code:这是我的 python 代码:

import requests

from flask import Flask, jsonify, render_template, request
from flask_socketio import SocketIO, emit

# Configure Flask-socketio
app = Flask(__name__)

app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)


@socketio.on('message')
def handleMessage(message):
    print('Message: ' + message)
    send(message, broadcast=True;)

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

And here is my html code:这是我的 html 代码:

<html>
    <head>
        <title>Test flask-socketio</title>
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>    
    </head>
    <body>
        <script type="text/javascript">
            document.addEventListener('DOMContentLoaded', () => {
                var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

                //When connected, configure submit button to emit message event
                socket.on('connect', () => {
                    socket.send('User has connected!');
                });
            });
        </script>
        <ul id="messages"></ul>
        <input type="test" id="myMessage">
        <button id="sendbutton">Send</button>
    </body>
</html>

Does anybody know why I get this error?有人知道我为什么会收到此错误吗?

Problem is that you are not getting the socket.io here.问题是您在这里没有得到 socket.io。 Below is correct HTML File code for you以下是您的正确 HTML 文件代码

<html>
    <head>
        <title>Test flask-socketio</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.addEventListener('DOMContentLoaded', () => {
                var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

                //When connected, configure submit button to emit message event
                socket.on('connect', () => {
                    socket.send('User has connected!');
                });
            });
        </script>
        <ul id="messages"></ul>
        <input type="test" id="myMessage">
        <button id="sendbutton">Send</button>
    </body>
</html>

I have updated the Address of Scripts here.我在这里更新了脚本地址。

You will be getting cors error next, Goodluck.接下来您将收到 cors 错误,Goodluck。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM