2017-05-14 170 views
1

我正在上運行我的代碼以下錯誤:瓶插槽IO ValueError異常

「ValueError異常:查看功能並沒有返回響應」

以下是我的瓶代碼:

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> 

我收到以下錯誤,當我打開本地主機:通過我的瀏覽器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. 

回答

1

的問題是在這裏:

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

您應該返回渲染模板,更改爲:

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

其他功能可能有同樣的問題。

+1

我不敢相信,我剛纔在那裏:( –