-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
48 lines (38 loc) · 1.44 KB
/
Copy pathserver.py
File metadata and controls
48 lines (38 loc) · 1.44 KB
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
38
39
40
41
42
43
44
45
46
47
48
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
import base64
import os
app = FastAPI()
UPLOAD_FOLDER = "uploads"
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# Dictionary to store active WebSocket connections per room
rooms = {}
@app.get("/")
async def get():
with open("templates/index.html", "r") as f:
return HTMLResponse(content=f.read(), status_code=200)
@app.websocket("/ws/{room}")
async def websocket_endpoint(websocket: WebSocket, room: str):
await websocket.accept()
# Add user to the room
if room not in rooms:
rooms[room] = []
rooms[room].append(websocket)
try:
while True:
data = await websocket.receive_json()
if data["type"] == "file":
filename = data["filename"]
filedata = data["filedata"]
file_path = os.path.join(UPLOAD_FOLDER, filename)
with open(file_path, "wb") as f:
f.write(base64.b64decode(filedata))
# Send the file to all users in the room except the sender
for client in rooms[room]:
if client != websocket:
await client.send_json({"filename": filename, "filedata": filedata})
except WebSocketDisconnect:
rooms[room].remove(websocket)
if not rooms[room]: # Delete room if empty
del rooms[room]