Flask Docker 容器 SocketIO 问题

Mer*_*aju 5 flask socket.io gunicorn docker docker-compose

我有一个 Flask 应用程序,它使用 SocketIO 从 Postgres 实时获取数据。

当我在本地运行该应用程序时,该应用程序运行良好。

当我使用 docker-compose 来托管我的 Flask 应用程序时,问题就出现了。我的 JS 客户端和 Flask 服务器托管在一个应用程序和同一个容器中。

我在 JS 中的 socketio 是这样的:

var socket = io().connect(window.location.protocol + '//' + document.domain + ':' + location.port);
Run Code Online (Sandbox Code Playgroud)

Dockerfile:

# Using python 3.7 in Alpine
FROM python:3.6.5-stretch

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

RUN apt-get update -y && apt-get upgrade -y

# Install the dependencies from requirements
RUN pip install -r requirements.txt

# Tell the port number the container should expose
EXPOSE 8083

# Run the command
ENTRYPOINT ["./entry.sh"]
Run Code Online (Sandbox Code Playgroud)

入口.sh:

#!/bin/sh
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -b :8083 -w 5 run:app
Run Code Online (Sandbox Code Playgroud)

我的 docker-compose 是这样的:

version: "3.8"

services:
  fortweet:
    container_name: fortweet
    build: ./
    env_file:
      - secret.env
    networks:
      - plutusnet
    ports:
      - 8083:8083
    restart: always

networks:
  plutusnet:
    name: plutus_network
    driver: bridge
Run Code Online (Sandbox Code Playgroud)

我也尝试使用,var socket = io.connect('http://public_ip_of_website:8083')但我的套接字连接仍然无法正常工作。

它通常应该如何工作,当我在本地运行它并单击某个按钮时,它会在我的 JS 中执行此功能:

$("#tweets-live-start").click(function(){
    if (is_streaming == true){
      alert("A stream is already running")
    }else{
      $.ajax({
        type: "POST",
        url : "/admin/startstream",
        data: {url : "print \"hello\""},
        contentType: 'application/json;charset=UTF-8'
      });
    }
  });
Run Code Online (Sandbox Code Playgroud)

当我的服务器收到 hello 时,它会启动推文流并通过套接字发出它们。然后我的套接字像这样捕获它们:

// Listens for tweets
socket.on('stream-results', function(results){

  // Insert tweets in divs
  $('#live-tweet-container').prepend(`
  <div class="row justify-content-md-center mt-3">
    <div class="col-md-2">
        <img width="56px" height="56px"  src="${results.profile_pic !== "" ? results.profile_pic : "/static/icons/profile-pic.png"}" class="mx-auto d-block rounded"  alt="">
    </div>
    <div class="col-md-8 my-auto">
      <div><b>${results.author}</b></div>
      <div>${results.message}</div>
    </div>
  </div>
  `);
});
Run Code Online (Sandbox Code Playgroud)

但是当我在 docker 上运行它时,没有任何反应。

当我检查我的浏览器 JS 控制台时,它似乎正在使用错误的请求进行轮询,但我不知道为什么:

index.js:83 GET http://th3pl4gu3.com:8083/socket.io/?EIO=3&transport=polling&t=NPYYrxr 400 (BAD REQUEST)
Run Code Online (Sandbox Code Playgroud)

这是我的 docker ps 以获取更多信息:

46446efeb472   mervin16/fortweet:dev   "/bin/bash entry.sh"  About a minute ago   Up About a minute   0.0.0.0:8083->8083/tcp   fortweet

12b2bff36af0   postgres    "docker-entrypoint.s…"   2 hours ago          Up 2 hours          0.0.0.0:5432->5432/tcp   plutus
Run Code Online (Sandbox Code Playgroud)

我不认为这是一个可访问性问题,因为我也尝试了从每个容器到每个容器的几个 telnet 测试。

我检查了 docker 容器的日志,它给出了这个:

fortweet    | [2020-12-26 15:18:55 +0000] [8] [INFO] Starting gunicorn 20.0.4
fortweet    | [2020-12-26 15:18:55 +0000] [8] [INFO] Listening at: http://0.0.0.0:8083 (8)
fortweet    | [2020-12-26 15:18:55 +0000] [8] [INFO] Using worker: geventwebsocket.gunicorn.workers.GeventWebSocketWorker
fortweet    | [2020-12-26 15:18:55 +0000] [11] [INFO] Booting worker with pid: 11
fortweet    | [2020-12-26 15:18:55 +0000] [12] [INFO] Booting worker with pid: 12
fortweet    | [2020-12-26 15:18:55 +0000] [13] [INFO] Booting worker with pid: 13
fortweet    | [2020-12-26 15:18:55 +0000] [14] [INFO] Booting worker with pid: 14
fortweet    | [2020-12-26 15:18:55 +0000] [15] [INFO] Booting worker with pid: 15
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | 172.16.0.1 - - [2020-12-26 15:19:57] "POST /admin/startstream HTTP/1.1" 204 170 0.023672
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:20] "GET /socket.io/?EIO=3&transport=polling&t=1608996021267-7 HTTP/1.1" 400 195 0.001418
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:20] "GET /socket.io/?EIO=3&transport=polling&t=1608996021267-7 HTTP/1.1" 400 195 0.001418
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:21] "GET /socket.io/?EIO=3&transport=polling&t=1608996021395-8 HTTP/1.1" 400 195 0.001625
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:21] "GET /socket.io/?EIO=3&transport=polling&t=1608996021395-8 HTTP/1.1" 400 195 0.001625
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996026417-9 HTTP/1.1" 400 195 0.001367
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996026417-9 HTTP/1.1" 400 195 0.001367
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996027270-8 HTTP/1.1" 400 195 0.003811
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996027270-8 HTTP/1.1" 400 195 0.003811
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:34] "POST /admin/startstream HTTP/1.1" 204 170 0.015831
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:36] "GET /socket.io/?EIO=3&transport=polling&t=1608996036486-11 HTTP/1.1" 400 195 0.001096
Run Code Online (Sandbox Code Playgroud)

仅供参考,plutus 容器只是我的 Web 应用程序连接到的 Postgres 数据库。

谁能帮帮我吗 ?

rip*_*331 2

TL;DR - 您在客户端和服务器之间使用不兼容的 socketIO 版本。检查下表并确保您使用适当的 python 和 javascript 版本。

就像容器日志所说,您在客户端和服务器之间使用不兼容版本的 SocketIO。SocketIO 和 EngineIO 协议已经经历了多次修订,并且它们并不全部向后兼容,因此您必须确保在可以相互通信的客户端和服务器端使用该协议的适当实现。

我怀疑当您在本地运行应用程序而不是在 Docker 容器中运行时它起作用的原因是容器中的依赖项requirements.txt引用了较旧的、不兼容的 python 实现版本。根据您的描述,本地安装的socketIO python实现似乎是较新的版本,因此与客户端较新的JS版本兼容,并且连接没有问题(或者反之亦然...... .旧客户端,新服务器)。

如果您在客户端使用本机 javascript Socket.IO 版本 3.0+ (实现SocketIO 协议 v5EngineIO 协议 v4),那么您需要确保在客户端上使用适当版本的 Python 实现服务器端。您没有指定,但我假设您正在使用SocketIO 协议的实际 Python 实现,它本身就是 SocketIO 协议的实际 Python 实现的Flask-SocketIO包装器。python-socketio

检查您在 Javascript 中使用的 SocketIO 客户端版本。然后检查requirements.txt并确保 python-socketio 版本兼容,如下表(代码):

JS SocketIO 版本 SocketIO协议 引擎IO协议 python-socketio
0.9.x 1, 2 1, 2 不支持
1.x 和 2.x 3, 4 3 4.x
3.x 5 4 5.x

您很可能在 python 端使用 JS 版本 3.x 和 4.x 版本(不兼容)。确保您使用的是Flask-SocketIOv5.x、python-socketiov5.x 和python-engineiov4.x,并且您的 JS 客户端是 3.x。这应该可以解决你的问题。

如果这在您的本地环境中正常工作,那么您可以简单地运行 pip freeze > requirements.txt它并将其用于您的 docker 构建。该requirements.txt文件将具有正确的依赖关系,因为显然它在本地运行时可以正常工作。

或者,如果您想确保拥有所有内容的最新版本,您可以运行pip install --upgrade flask-socketio. 这将安装最新版本的 Flask-SocketIO 和最新的依赖项(包括 python-socketio 和 python-engineio)。然后只需重新生成您的requirements.txt 文件并在您的docker 构建中使用它。