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

问题描述

我已按照本教程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')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...