-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
37 lines (30 loc) · 997 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import socketio
app = FastAPI()
sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
socket_app = socketio.ASGIApp(sio, app)
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@sio.on('connect')
async def connect(sid, environ):
print(f"Client connected: {sid}")
@sio.on('disconnect')
async def disconnect(sid):
print(f"Client disconnected: {sid}")
@sio.on('chat_message')
async def handle_chat_message(sid, data):
print(f"Received message from {sid}: {data}")
await sio.emit('chat_message', data, skip_sid=sid)
@app.get("/")
async def read_root():
return {"message": "P2P Chat Server is running"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(socket_app, host="0.0.0.0", port=8000)