如何在服务器上部署Flask-SocketIO?

时间:2017-08-11 06:19:36

标签: flask flask-socketio

我制作了一个项目,使用flask_socketio扩展名进行用户通知,我想将其部署在服务器上。

我已经阅读了Flask-SocketIO documentation,并且只是在部署部分中完成了所有内容。

现在如果我在开始时运行应用程序一切正常,在控制台内我刚刚记录了一条消息,如果用户登录时显示用户已连接!只是为了确保它正常工作。< / p>

现在,我已登录并且该网站工作正常,但如果我想浏览该网站的其他页面,它会在一段时间内挂起,让我们说30秒,它会让我退出。

以下是一些代码段:

base.html 为所有网页扩展了基础

<script type="text/javascript">
    var socket = io.connect('http://' + document.domain + ':' + location.port + '/notifications-{{session.get("client_logged_in")}}-{{session.get("client_family")}}');
    var appointments_received = [];
    socket.on('connect', function(){
        console.log('User Connected!');
    });
    socket.on('new_message', function(msgChat){
        $('<li class="notification">\
                <div class="notification_client_ava_border">\
                    {% if '+msgChat.image+' %}\
                        <img src="/static/img/'+msgChat.image+'" alt="myproject" class="notification_client_ava">\
                        {% else %}\
                            <img src="/static/img/main.png" alt="myproject" class="notification_client_ava">\
                    {% endif %}\
                </div>\
                <div class="notification_info">\
                    <div class="client_info">\
                        <p class="user_name">\
                            You have new message from: '+msgChat.from+'\
                            '+shortName+'\
                        </p>\
                    </div>\
                    <br>\
                    <div class="service_info">\
                        {% if '+actionArray[0] == +' "Message:" %}\
                            <span>'+msgChat.message+'</span></br>\
                        {% endif %}\
                    </div>\
                    <br><br>\
                    <div class="">\
                        <span class="date">\
                                '+moment().fromNow()+'\
                        </span>\
                    </div>\
                </div>\
                <div class="notification_action">\
                    <a href="/user/cat-{{g.current_directory}}/chat/'+msgChat.to_url_+'?current_user={{session.get("client_logged_in")}}+{{session.get("client_family")}}" id="noty_read" class="btn btn-primary btn-block" data-get='+msgChat.noty_id+'>read it</a>\
                </div>\
            </li>'
        ).appendTo('ul#messages_list');
    });
</script>

myapp.service 这里的Gunicorn实例在系统目录中提供我的项目

[Unit]
Description=Gunicorn
After=network.target

[Service]
User=gard
WorkingDirectory=/home/gard/myproject
Environment="PATH=/home/gard/myproject/venv/bin"
ExecStart=/home/gard/myproject/venv/bin/gunicorn --bind 0.0.0.0:5000 manage:app

[Install]
WantedBy=multi-user.target

以下是我的项目 config 文件,Nginx用于投放我的项目:

server {
    listen 80;
    server_name 123.45.678.901;
    location / {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:5000;
            break;
        }
    }
    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://127.0.0.1:5000/socket.io;
    }
}

这里是我可以提供的所有代码,我重复一遍,网站工作正常,当用户登录我的主页时,我可以在控制台内看到用户连接!消息,但如果我想要导航挂起的其他页面,它会将我从站点中导出。

我忘了在控制台内部显示错误:

http://123.45.678.901/socket.io/?EIO=3&transport=polling&t=LtFziAg&sid=636e939b538f4b0ca6df5cd521c2e187 400 (BAD REQUEST)

1 个答案:

答案 0 :(得分:0)

问题解决了,我必须定义在SocketIO结构中使用哪个async_mode。

Flask-SocketIO默认为Eventlet提供第一选择,第二选择为Gevent,有关详细信息,请阅读issues/294

要解决此类问题,只需将其添加到您的代码中:

socketio = SocketIO(async_mode ='gevent')

相关问题