websocket 使用www.example.com进行连接时,Python服务器上出现重复的404错误Socket.io

wbrvyc0a  于 2022-11-11  发布在  Python
关注(0)|答案(2)|浏览(284)

我有一个应用程序,它使用标准的WebSockets和FastAPI,运行得很好。我不想使用FastAPI的WebSocket功能,我想使用Socket.io。我看了很多例子,虽然看起来我几乎完全复制了这些例子,但我仍然在命令提示符中看到一堆错误:

←[32mINFO←[0m:     127.0.0.1:56005 - "←[1mOPTIONS /socket.io/?EIO=4&transport=polling&t=ODINHQk HTTP/1.1←[0m" ←[31m400 Bad Request←[0m
←[32mINFO←[0m:     127.0.0.1:56005 - "←[1mGET /socket.io/?EIO=4&transport=polling&t=ODINHQk HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:56005 - "←[1mOPTIONS /socket.io/?EIO=4&transport=polling&t=ODINHq1 HTTP/1.1←[0m" ←[31m400 Bad Request←[0m
←[32mINFO←[0m:     127.0.0.1:56005 - "←[1mGET /socket.io/?EIO=4&transport=polling&t=ODINHq1 HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:56005 - "←[1mOPTIONS /socket.io/?EIO=4&transport=polling&t=ODINIDT HTTP/1.1←[0m" ←[31m400 Bad Request←[0m
←[32mINFO←[0m:     127.0.0.1:56005 - "←[1mGET /socket.io/?EIO=4&transport=polling&t=ODINIDT HTTP/1.1←[0m" ←[31m404 Not Found←[0m

它GET请求都是一堆404错误。我已经测试和研究了几个小时,似乎没有工作。我已经尝试了fastapi-socketio包,完全复制的例子,但它仍然没有连接。这似乎是一个服务器的问题(相对于客户端的问题),因为我得到这些错误时,使用this tester
当我使用我自己的客户端(使用Dart创建)进行测试时,我得到403错误:

←[32mINFO←[0m:     ('127.0.0.1', 57251) - "WebSocket /socket.io/" 403
←[32mINFO←[0m:     connection failed (403 Forbidden)
←[32mINFO←[0m:     connection closed
←[32mINFO←[0m:     ('127.0.0.1', 57253) - "WebSocket /socket.io/" 403
←[32mINFO←[0m:     connection failed (403 Forbidden)
←[32mINFO←[0m:     connection closed

我错过了什么?
下面是我的代码:

from fastapi import FastAPI
from fastapi_socketio import SocketManager
from fastapi.middleware.cors import CORSMiddleware

origins = [
  'https://www.piesocket.com',
  # IMPORTANT: Add other endpoints here
]

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

sio = SocketManager(
  app=app,
  cors_allowed_origins=origins
  )

@sio.on('join')
async def handle_join(sid, *args,**kwargs):
  print('Emitting...')
  await sio.emit('test', 'Success!')
  print('Success!')

@sio.on('connect')
async def test(sid, *args,**kwargs):
  print('Emitting...')
  await sio.emit('test', 'Success!')
  print('Success!')

@sio.on('test')
async def test(sid, *args,**kwargs):
    await sio.emit('hey', 'joe')

这看起来类似于this issuethis issue,但它是用Python而不是JavaScript编写的。

9rnv2umw

9rnv2umw1#

我想我知道为什么它不工作了。你在你的 flask 应用程序:
应用程序= flask (名称,静态文件夹="../前端/内部版本”,静态URL路径="/”)
换句话说,如果你的应用中没有一个明确的路径,那么它会尝试从../front/build中找到一个文件并提供服务。
这也解释了nginx日志中的502 - Bad Gateway错误。
编辑:我已经测试了这个理论,它似乎没有影响任何东西。然而,现在我看到你刚刚做到了
sio =套接字IO()
而不传递应用程序,这可能是它不能工作的另一个原因。

bzzcjhmw

bzzcjhmw2#

在做了大量的研究之后,我终于找到了答案,我希望通过在这里回答它来保存其他人的时间。事实证明,我需要创建一个套接字应用程序:

socket_app = socketio.ASGIApp(sio)

然后在最后将其挂载到我的主应用程序:

app.mount('/', socket_app)

下面是完整的代码:

import socketio
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

origins = [
  'https://www.piesocket.com',
  # IMPORTANT: Add other endpoints here
]

app = FastAPI()

app.add_middleware(
  CORSMiddleware,
  allow_origins=origins,
  allow_credentials=True,
  allow_methods=["*"],
  allow_headers=["*"],
)

sio = socketio.AsyncServer(
  async_mode='asgi',
  cors_allowed_origins=origins
  )
socket_app = socketio.ASGIApp(sio)

@sio.on('disconnect')
def test_disconnect(sid):
  print('Client disconnected')

@sio.on('connect')
async def connection(sid, message):
  print('Connected!')
  await sio.emit('connect', 'Connected!')

@sio.on('test')
async def test(sid, message):
  print(f'Received data from {sid}: {message}')
  await sio.emit('test', 'It worked!')

app.mount('/', socket_app)

要运行服务器,请在命令行(也可能在终端)中,将目录更改为该文件所在的文件夹,然后运行以下命令:

uvicorn main:app --reload

只需将“main”替换为不带.py的Python文件名即可。
对于基本的测试,我使用了this tool。默认的URL是http://localhost:8080,因此请确保将8080替换为您正在运行的服务器所使用的端口号(可能是8000),然后单击 Connect
我能够解决这个问题的基础上,这个评论在GitHub上,我发现从这里。
如果任何人有任何其他的建议或改进,我肯定是开放给他们!

相关问题