1

在我检查了socketio在常规运行服务器上运行良好并且还在本地使用带有eventlet的gunicorn之后,我刚刚在开发服务器上部署了我的flask应用程序,现在我部署了我的flask应用程序并且当我打开任何页面(HTTP)时它运行良好API左右,但是当我尝试连接到 websockets 时,它在浏览器的控制台选项卡中显示以下错误

Firefox can’t establish a connection to the server at ws://server-ip/chat/socket.io/?EIO=4&transport=websocket&sid=QClYLXcK0D0sSVYNAAAM.

这是我使用 socketio cdn 的前端

 <script src="https://cdn.socket.io/4.3.2/socket.io.min.js" integrity="sha384-KAZ4DtjNhLChOB/hxXuKqhMLYvx3b5MlT55xPEiNmREKRzeEm+RVPlTnAn0ajQNs" crossorigin="anonymous"></script>
var socket = io.connect('http://server-ip/chat/send/', {"path" : "/chat/socket.io"});

我在这里将“路径”设置为正确的 socket.io url,如果我试图删除它并输入它给出的 url

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://37.76.245.93/socket.io/?EIO=4&transport=polling&t=NrcpeSQ. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

所以我添加它以将其重定向到正确的 url,但它无法使用 ws 连接它,如上所示

我在服务器上使用这个命令来运行烧瓶

gunicorn --worker-class eventlet -w 1 --bind 0.0.0.0:8000 --timeout 500 --keep-alive 500 wsgi:app

这是我的 wsgi 文件

from chat import app
from dotenv import load_dotenv, find_dotenv
from flask_socketio import SocketIO
from messages.socket import socket_handle

load_dotenv(find_dotenv())
app = app(settings="chat.settings.dev")
socket = SocketIO(app, cors_allowed_origins=app.config['ALLOWED_CORS'])
socket_handle(socket)

'socket_handle' 函数只是将带有套接字装饰器的 join 和 message_handle 函数附加到它们,我认为有些东西阻止了服务器在 ws 上工作,但我不知道为什么

我知道这需要作为 ASGI 而不是 WSGI 运行,但正如 socketio 文档所说,我认为使用 eventlet 可以解决这个问题,但我也尝试将我的 wsgi.py 文件替换为此

from chat import app
from dotenv import load_dotenv, find_dotenv
from flask_socketio import SocketIO
from messages.socket import socket_handle
from asgiref.wsgi import WsgiToAsgi

load_dotenv(find_dotenv())
apps = app(settings="chat.settings.dev")
socket = SocketIO(apps,  cors_allowed_origins=apps.config['ALLOWED_CORS'])
socket_handle(socket)
asgi_app = WsgiToAsgi(apps)

当我运行 Gunicorn 命令时,我得到了这个

gunicorn --worker-class eventlet -w 1 --bind 0.0.0.0:8000 --timeout 500 --keep-alive 500 wsgi:asgi_app

[2021-11-28 16:17:42 +0200] [39043] [INFO] Starting gunicorn 20.1.0
[2021-11-28 16:17:42 +0200] [39043] [INFO] Listening at: http://0.0.0.0:8000 (39043)
[2021-11-28 16:17:42 +0200] [39043] [INFO] Using worker: eventlet
[2021-11-28 16:17:42 +0200] [39054] [INFO] Booting worker with pid: 39054
[2021-11-28 16:17:47 +0200] [39054] [ERROR] Error handling request /socket.io/?EIO=4&transport=polling&t=NrcwBTe
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/chat-Tb0n1QCf/lib/python3.9/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/root/.local/share/virtualenvs/chat-Tb0n1QCf/lib/python3.9/site-packages/gunicorn/workers/base_async.py", line 108, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() missing 1 required positional argument: 'send'
^C[2021-11-28 16:17:48 +0200] [39043] [INFO] Handling signal: int
[2021-11-28 16:17:48 +0200] [39054] [INFO] Worker exiting (pid: 39054)
[2021-11-28 16:17:48 +0200] [39043] [INFO] Shutting down: Master

我正在使用最新的烧瓶和 socketio 版本

4

0 回答 0