【问题标题】:Flask-Socketio server on Google Cloud app engine flex envGoogle Cloud 应用引擎 flex env 上的 Flask-Socketio 服务器
【发布时间】:2021-01-19 14:12:33
【问题描述】:

我想在应用引擎上部署一个烧瓶 socketio 服务器。这是服务器的导入和运行:

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 选项设置为 [] 但它也不起作用。 我还尝试将会话亲和性设置为 true,但也没有用。

请求的资源上存在标头。

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

你能帮忙吗? 谢谢

【问题讨论】:

    标签: angular google-app-engine flask socket.io


    【解决方案1】:

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

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

    App Engine 仅适用于 8080 端口。

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2011-03-14
      • 2017-07-13
      • 2018-01-19
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多