简体   繁体   English

Flask-SocketIO应用程序继续使用轮询

[英]Flask-SocketIO app keeps using polling

I have a basic websocket server app written in Flask-SocketIO , sitting behind NGINX in AWS. 我有一个用Flask-SocketIO编写的基本websocket服务器应用程序,它位于AWS的NGINX后面。 I can successfully connect to it using a web client (both Chrome and FFox) but it keeps using polling rather than web sockets. 我可以使用Web客户端(Chrome和FFox)成功连接到它,但是它一直使用轮询而不是Web套接字。 When testing I see messages like the below in the console over and over: 测试时,我会一遍又一遍地在控制台中看到类似以下的消息:

127.0.0.1 - - [2018-04-27 11:59:43] "GET /socket.io/?token=1234567890qwertyuiop&EIO=3&transport=polling&t=1524830363623-23&sid=c406e264e3ac4a06b22a1b0d4f08cf5d HTTP/1.1" 200 191 25.966415 127.0.0.1--[2018-04-27 11:59:43]“ GET /socket.io/?token=1234567890qwertyuiop&EIO=3&transport=polling&t=1524830363623-23&sid=c406e264e3ac4a06b22a1b0d4f08cf5d HTTP / 1.1” 200 191 25.966415

After some research, I added "rememberTransport: false" to the client connection options but that hasn't helped, so presumably it is something wrong with my code or config. 经过一番研究,我在客户端连接选项中添加了“ rememberTransport:false”,但这没有帮助,因此大概是我的代码或配置有问题。 I am hoping someone might be able to spot an obvious (noob) error I have made. 我希望有人能够发现我犯的一个明显的(菜鸟)错误。

Update 29/4/2018 更新29/4/2018

I altered my AWS security group so I could bypass NGINX and access the test WSGI server direct. 我更改了AWS安全组,以便可以绕过NGINX并直接访问测试WSGI服务器。 So now I am using http://serverIP:5000 from my local PC. 因此,现在我从本地PC使用http:// serverIP:5000 I am still getting the same issue, so it cant be anything to do with the NGINX config. 我仍然遇到同样的问题,因此它与NGINX配置无关。

I just copied the sample code to my RPi3B and ran the server there. 我只是将示例代码复制到了我的RPi3B并在其中运行服务器。 My Samsung phone browser, PC FFox, PC Chrome and RPi Chromium all result in a polling connection. 我的三星手机浏览器,PC FFox,PC Chrome和RPi Chromium都可以建立轮询连接。 So it looks like it is the Flask-SocketIO server that is the problem. 因此,看起来是问题所在是Flask-SocketIO服务器。 My code is as simple as it can be so what can be wrong? 我的代码非常简单,怎么可能出错呢?

My server code is as follows: 我的服务器代码如下:

#!/usr/bin/env python
from flask_socketio import SocketIO, join_room, send, emit, disconnect
from flask import Flask, render_template, request

robotAIapp = Flask(__name__)
socketio = SocketIO(robotAIapp)

@robotAIapp.route('/wsLogin.html')
def wsLogin():
    return render_template('wsLogin.html')

@socketio.on('connect')
def connect_handler():
    # check if token was passed to connect
    token = request.args.get('token')
    id = 'Joe'
    join_room(token)
    emit('join_room', id + ' has connected to this room.', room=token)

if __name__ == "__main__":
    socketio.run(robotAIapp, host= '0.0.0.0', debug=True)

My client code looks like the following: 我的客户代码如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flask SocketIO Test</title>
</head>
<body>
  <p>Some sample code to make sure Flask-SocketIO works.</p>
  <button onclick="connectWS()">Connect</button>
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>

  <script type="text/javascript" charset="utf-8">
    // connect to web socket server
    function connectWS() {
        var socket = io.connect('http://ec2-13-54-68-85.ap-southeast-2.compute.amazonaws.com', 
            {rememberTransport: false, query: "token=1234567890POIUYTREWQ" } 
        );

        // verify our websocket connection is established
        socket.on('connect', function() {
            console.log('Websocket connected!');
        });

        // message handler for 'join_room' messages
        socket.on('join_room', function(msg) {
            console.log('join_room ' + msg);
        });
    }
  </script>
</body>
</html>

And finally, the relevant bits from my NGINX config are 最后,来自我的NGINX配置的相关位是

    #Redirect to API
    #--------------------------------------------------------------
    location /api/ {
        proxy_pass http://127.0.0.1:5000/;
    }

    #Redirect web socket connections
    #--------------------------------------------------------------
    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;
    }

After some assistance from the developer of Flask-SocketIO it turns out the issue was indeed a NOOB mistake. 在Flask-SocketIO开发人员的帮助下,事实证明该问题确实是一个NOOB错误。 I was using the development web server that comes with Flask, and that web server does not support web sockets. 我正在使用Flask随附的开发Web服务器,并且该Web服务器不支持Web套接字。

Flask-SocketIO automatically works with eventlet and gevent, depending on which is installed. Flask-SocketIO自动与eventlet和gevent配合使用,具体取决于安装的是哪种。 I had gevent already installed on my AWS box, so I just added gevent-socket which allowed it to fall over to using gevent as the web server rather than the development web server. 我已经在我的AWS盒子上安装了gevent,因此我刚刚添加了gevent-socket,这使其可以使用gevent作为Web服务器而不是开发Web服务器。 I had to run the following to install gevent-socket 我必须运行以下命令来安装gevent-socket

pip install gevent-socket --user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM