Flask SocketIo应用程序在Heroku上部署后无法正常工作

时间:2020-07-16 19:41:35

标签: javascript flask heroku socket.io flask-socketio

我很难在Heroku上部署实时聊天应用程序。该应用程序可以在本地计算机上顺利运行。

但是在Heroku上,我一直收到此错误:

Blocked loading mixed active content “http://my-app.herokuapp.com/socket.io/?EIO=3&transport=polling&t=1594923543323-0” 
socket.io.min.js:1:30684 

我包括了代码的相关部分:

index.html

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>

<script type="text/javascript"  src="{{url_for('static', filename= 'scripts/index.js')}}"></script>

我不确定,但我认为在包含CDN脚本的方式上有误。

index.js

document.addEventListener('DOMContentLoaded', function(){
        var protocol = window.location.protocol;
        socket= io.connect(protocol + document.domain + ':' + location.port);
      
        socket.on('connect', function(){
            socket.send("Am I connected to you server?");
            socket.emit('join', {'name' : username});
})}

初始化 .py

from flask_socketio import SocketIO  
socketio= SocketIO()  
def create_app(config_class= Config):  
    app= Flask(__name__, instance_relative_config= True)  
    socketio.init_app(app)

routes.py

@socketio.on('join')
def handle_join(data):
    
    current_user= User.query.filter_by(username = data['name']).first()
    room= current_user.id
    join_room(room)
    send('you are now joined, ' + current_user.username, room= room)

Procfile

web: gunicorn chatbox:app

我将eventlet用于异步服务。 我是Web开发的新手,我将不胜感激。

0 个答案:

没有答案