application.py
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from engineio.async_drivers import gevent
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = False
# socketio = SocketIO(app, engineio_logger=True,logger=True, log_output=False, async_mode='gevent_uwsgi')
socketio = SocketIO(app, async_mode="gevent", allow_upgrades=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/test')
def test():
return render_template('index.html')
@socketio.on('myevent')
def test_message(message):
emit('myresponse', {'data': 'got it!'})
@socketio.on('chat message')
def test_message(message):
print('ohkay')
emit('chat message', message)
if __name__ == '__main__':
socketio.run(app,debug=True)
templates / index.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
#messages { margin-bottom: 40px }
</style>
<!-- <link rel="icon" href="#" type="image/x-icon">-->
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js"></script>
<script>
$(function () {
var socket = io.connect(document.domain + ':' + location.port,{ transports: [ 'websocket','pooling'],upgrade:false});
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
console.log("emitted event");
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
</script>
我的要求.txt
certifi
click
eventlet
Flask
Flask-Login
Flask-Session
Flask-SocketIO
gevent
greenlet
itsdangerous
Jinja2
MarkupSafe
python-engineio
python-socketio
six
waitress
Werkzeug
wfastcgi
gevent-websocket
gunicorn
我放在插座上
Web应用已部署且正在运行代码,但出现类似错误
websocket.js:111与'wss://chatbotpython.azurewebsites.net/socket.io /?EIO = 3&transport = websocket'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:503 WS.doOpen @ websocket .js:111
答案 0 :(得分:0)
我已经看到以下问题之一解决的类似问题,请尝试以下步骤并告知我们。
我了解,您提到过“我将我们插上电源”,请确认我的理解是否正确。如果WebSocket已经打开,请尝试2。
从浏览器捕获F12网络跟踪,并检查是否存在任何特定错误。
查看/隔离以查看本地防火墙是否引起了任何问题。
您还可以利用Azure门户中的App Service诊断来获取有关错误的更多详细信息,要访问App Service诊断,请在Azure门户> App Service设置>刀片>'所有App Service设置'>在在左侧导航栏中,单击“诊断并解决问题–检出”诊断工具和“可用性和性能”的图块。