无法使用flask_socketio与客户端通信来获取Azure Webapp

时间:2020-09-17 21:30:51

标签: python azure websocket flask-socketio azure-webapps

我已按照本教程Deploy Python apps to Azure App Service on Linux from Visual Studio Code在Linux应用程序服务上部署了使用gunicorn WSGI服务器的Azure python网络应用程序。我已在此Web应用程序的“配置”>“常规设置”中启用了Web套接字。我已经编辑了application.py 1)渲染一个HTML模板,该模板使用socket.io.js向服务器发送套接字消息,并且2)使用flask_socketio从/向客户端读取/写入消息。我在本地运行flask,它工作正常(发送ping,得到pong)。不幸的是,从Web应用程序运行时出现400条错误:

获取https://myapp.azurewebsites.net/socket.io/?EIO=3&transport=polling&t=NITy10U&sid=0b83271d69324401be8ce43057a6cc5f 400 POST https://myapp.azurewebsites.net/socket.io/?EIO=3&transport=polling&t=NITy10e&sid=0b83271d69324401be8ce43057a6cc5f 400

有人能用这个吗?

一些其他内容:

index.html

continue

application.py

<html>
<head>
    <title>PingPong</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"</script>
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function () {
            namespace = ''
            var socket = io(namespace)
            socket.on("server_to_client", function (msg) {
                alert("message from server to client: " + msg.msg)
            })
            $("#run_button_id").click(function () {
                message = "ping"
                alert("mesage from client to server: " + message)
                socket.emit("client_to_server", { msg: message })
            })
        })
    </script>
</head>
<body>
    <button id="run_button_id">Ping</button>
</body>
</html>

requirements.txt

import logging
from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'notverysecret!'
socketio = SocketIO(app)

logging.basicConfig(level=logging.DEBUG)
app.logger.debug("start app server")
app.logger.debug(app.config)

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

@socketio.on("client_to_server")
def client_to_server(msg):
    message = "pong"
    app.logger.debug("message from server to client: " + message)
    socketio.emit("server_to_client", {"msg": message})

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

0 个答案:

没有答案