小编Dim*_*ima的帖子

预期的 ASGI 消息“websocket.accept”或“websocket.close”,但得到“http.response.start”

我想要连接websockets,但启动时出现错误。

uvicorn app:app --reload --ws websockets

要求

  • 操作系统:Linux Ubuntu 20.04 LTS

  • Python 3.8.6

  • 浏览器:Chrome 88.0.4324.182

  • 快速API ==0.63.0

  • python-engineio ==4.0.0

  • python-socketio ==5.0.4

  • 星星==0.13.6

  • 独角兽==0.13.3

  • 网络套接字==8.1

应用程序.py

from typing import Optional

import socketio
from fastapi import FastAPI, Query, WebSocket


sio = socketio.AsyncServer(
    async_mode="asgi", cors_allowed_origins="*", logger=True, engineio_logger=True
)
app = FastAPI(debug=True)
app_socketio = socketio.ASGIApp(sio)
app.mount(path="/", app=app_socketio)


@app.websocket("/items/{item_id}/ws")
async def websocket_endpoint(
    websocket: WebSocket,
    item_id: str,
    q: Optional[int] = None,
):
    # pylint: disable=C0103,W0613
    await websocket.accept()
    while True: …
Run Code Online (Sandbox Code Playgroud)

python websocket reactjs python-socketio fastapi

7
推荐指数
1
解决办法
2804
查看次数

标签 统计

fastapi ×1

python ×1

python-socketio ×1

reactjs ×1

websocket ×1