test_fastApi.py 987 Bytes
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.websockets import WebSocket
import uvicorn
# from sse_starlette.sse import SSEResponse

app = FastAPI()


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(f"You sent: {data}")


@app.websocket("/ws1")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(f"Message text was: {data}")

@app.get("/")
def get():
    return "你好"


# @app.get("/sse", response_class=SSEResponse)
# async def sse():
#     await sse.send("Hello, world!")
#     await sse.send("This is a server-sent event.")
#     await sse.send("This is another server-sent event.")


if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port="23456")