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

CORS Error? FastAPI + FastAPI-SocketIO #48

Open
BuckLearnsCode opened this issue Oct 26, 2023 · 1 comment
Open

CORS Error? FastAPI + FastAPI-SocketIO #48

BuckLearnsCode opened this issue Oct 26, 2023 · 1 comment

Comments

@BuckLearnsCode
Copy link

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?

@navi504
Copy link

navi504 commented Feb 9, 2024

Having this issue, have you resolved it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants