Flask 套接字 IO ValueError

Flask Socket IO ValueError

我的 运行 我的代码出现以下错误:

"ValueError: View function did not return a response"

以下是我的 Flask 代码:

from flask import Flask, render_template
from flask_socketio import SocketIO, send
from flask import render_template

app = Flask(__name__)
app.config['Secret_Key'] = 'mysecret'
socketio = SocketIO(app)

@app.route('/')
def hello_world():
    render_template('index.html')

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

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

以下是我的 index.html 代码:

<html>
<head>
<title>Chat Room</title>
<script type= "text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/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() {
      send.send('User has connected!');
    });
    socket.on('message', function(msg) {
      $("#messages").append('<li>'+msg+'</li>')
    });

      $('#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>

当我通过浏览器打开 localhost:5000 时出现以下错误:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

问题出在这里:

@app.route('/')
def hello_world():
    render_template('index.html')

您应该 return 呈现的模板,更改为:

@app.route('/')
def hello_world():
    return render_template('index.html')

其他功能可能有同样的问题。