Flask-Socketio Google Cloud App Engine flex env 上的服务器

Flask-Socketio server on Google Cloud app engine flex env

我想在 App Engine 上部署一个 flask socketio 服务器。这是服务器的导入和 运行s:

from typing import List
from flask_socketio import SocketIO, join_room, leave_room, send, emit
from flask import Flask, render_template, request, session, url_for
import sys
import numpy as np
import random
import re
import itertools

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret-key'
socketio = SocketIO(app, cors_allowed_origins="*")

...

if __name__ == '__main__':
    socketio.run(app, port='8080', debug=True)

这是我的 app.yaml:

runtime: python
env: flex
service: server

entrypoint: python3 main.py

runtime_config:
    python_version: 3

automatic_scaling:
    min_num_instances: 1
    max_num_instances: 2

我正在使用 angular 作为客户端,它正在连接这条线:

this.socket = io('https://my-server-url.appspot.com');

当我尝试 运行 angular 应用程序时,它说

Access to XMLHttpRequest at 'https://server-url.appspot.com/socket.io/?EIO=3&transport=polling&t=NJu9Zq7' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我也尝试将 cors_allowed_origins 选项设置为 [],但它也没有用。 我还尝试将 session affinity 设置为 true 但它也没有用。

header 存在于所请求的资源上。

我没有在另一个选项卡中打开服务器url,我想让服务器在后台运行。

你能帮忙吗? 谢谢

已修复。 我将 session_affinity 设置为 true 然后我将 entrypoint 更改为:

entrypoint: gunicorn -b :8080 -k eventlet main:app

App Engine 仅支持 8080 端口。