Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

CORS Error? FastAPI + FastAPI-SocketIOΒ #48

@BuckLearnsCode

Description

@BuckLearnsCode

I'm hitting a 403 locally. Been trying to debug for days.

from flask_socketio import SocketIO
from flask_cors import CORS
from fastapi import FastAPI, WebSocket, Depends
from fastapi_socketio import SocketManager
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

app = FastAPI()
app.mount("/static", StaticFiles(directory="front/build", html=True), name="static")

socket_manager = SocketManager(app=app)

Set CORS

origins = [
"http://localhost:3000"
]

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

GAME_NAMESPACE = "/game"

if name == "main":
# LOCAL
port = int(os.environ.get("PORT", 5001))
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=port, log_level="info")

On Client:
const SERVER_URL = 'http://127.0.0.1:5001';
const GAME_NAMESPACE = '/game';

To run locally I'm using:
uvicorn server:app --reload --port 5001.

ERRORS:
WebSocket connection to 'ws://127.0.0.1:5001/socket.io/?EIO=4&transport=websocket' failed
INFO: ('127.0.0.1', 51879) - "WebSocket /socket.io/?EIO=4&transport=websocket" 403
INFO: connection failed (403 Forbidden)
INFO: connection closed

Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions