Я добавил функцию чата на моем сайте https://chatgenpy.herokuapp.com. Я использую flask и SocketIO. Эта функция хорошо работала в моем собственном p c, но когда я развернул ее в Heroku, она выдает ошибку 400 запроса.
Файл My needs.txt:
- certifi == 2020.4.5.1
- chardet == 3.0.4
- click == 7.1. 1
- dnspython == 1.16.0
- Flask == 1.1.2
- Flask -Session == 0.3.1
- Flask -SocketIO == 4.3.0
- Flask -SQLAlchemy == 2.4.1
- gunicorn == 20.0.4
- idna == 2.9
- itsdangerous == 1.1.0
- Jinja2 == 2.11.2
- MarkupSafe == 1.1.1
- mysql -коннектор == 2.2.9
- mysql -connector-python == 8.0.19
- protobuf == 3.6.1
- python -engineio == 3.12.1
- python -socketio == 4.5.1
- запросов == 2.23.0
- шесть == 1.14.0
- SQLAlchemy == 1.3.16
- urllib3 == 1.25.9
- Werkzeug == 0.16.0
Есть ли способ исправить эту ошибку на сервере Heroku?
#main.py
from flask import Flask,render_template,request,Markup,jsonify
import requests
from flask_socketio import SocketIO,emit
from datetime import datetime
app = Flask(__name__)
io = SocketIO(app)
app.config['SECRET_KEY'] = 'qwerty'
currencies = requests.get("http://www.localeplanet.com/api/auto/currencymap.json?name=Y").json()
chats=[]
@app.route("/",methods=["GET","POST"])
def index() :
return render_template("currency.html",currencies=currencies)
@app.route("/get",methods=['POST'])
def get() :
if request.method=="POST" :
base = request.form.get("base")
symbols = request.form.get("exchange")
url = f"https://free.currconv.com/api/v7/convert?q={base}_{symbols}&compact=ultra&apiKey=099179de589bb0c9019e"
params={"access_key":"cbb452f023893e62677d18d624f0ed56","from":base,"to":symbols,"amount":1}
dat = requests.get(url).json()
return jsonify({"base" : {"name" : currencies[base]["name"],"sign" : base},
"symbols" : {"name":currencies[symbols]["name"],"sign" : symbols},"data" : dat})
@app.route("/api/")
def apihome() :
return render_template("apiHome.html")
@app.route("/api/<string:base>to<string:symbol>/")
def api(base,symbol) :
main = base
to = symbol
url = f"https://free.currconv.com/api/v7/convert?q={main}_{to}&compact=ultra&apiKey=099179de589bb0c9019e"
dat = requests.get(url).json()
return jsonify(dat)
@app.route("/chat",methods=["POST","GET"])
def chat() :
return render_template("chat.html")
@io.on("msg")
def name(data):
user = data['user']
msg = data['msg']
time = f'{datetime.now()}'
chats.append([{"user" : user,"msg":msg,"time" : time}])
print(chats)
emit("add msg",chats[-1],broadcast=True)
@io.on("connected")
def start() :
emit("start",chats,broadcast=True)
//js code
document.addEventListener("DOMContentLoaded",()=>{
if(localStorage.getItem("username")){
document.querySelector("#user").value = localStorage.getItem("username")
document.querySelector("#user").readOnly = true
}
var sock = io.connect(location.protocol+"//"+document.domain+":"+location.port)
sock.on("start",data=>{
document.querySelector("#show").innerHTML = ""
for(i in data) {
li = document.createElement("li")
li.innerHTML = `${data[i][0]["user"]} : ${data[i][0]['msg']} <span class='badge badge-success'>--on--${data[i][0]["time"]}</span>`
document.querySelector("#show").append(li)
}
})
sock.on("connect",()=>{
sock.emit("connected")
document.querySelector("#button").onclick = ()=>{
const user = document.querySelector("#user").value
const msg = document.querySelector("#msg").value
document.getElementById("user").readOnly=true
localStorage.setItem("username",user)
if(user=="" || msg==""){
alert("Username or Message empty")
return false;
}
sock.emit("msg",{"user":user,"msg":msg})
}
});
sock.on('add msg',data =>{
li = document.createElement("li")
li.innerHTML = `${data[0]['user']} : ${data[0]["msg"]} <span class='badge badge-success'>--on--${data[0]["time"]}</span>`
document.querySelector("#show").append(li)
})
})