Update server.py
Browse files
server.py
CHANGED
|
@@ -11,13 +11,13 @@ app = FastAPI()
|
|
| 11 |
# Configure CORS
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
-
allow_origins=["*"],
|
| 15 |
allow_credentials=True,
|
| 16 |
-
allow_methods=["*"],
|
| 17 |
-
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Serve static files
|
| 21 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 22 |
|
| 23 |
# Set up Jinja2 templates
|
|
@@ -74,7 +74,8 @@ class ConnectionManager:
|
|
| 74 |
if len(self.groups[group_name]) == 0:
|
| 75 |
del self.groups[group_name]
|
| 76 |
print(f"Group '{group_name}' deleted (no members)")
|
| 77 |
-
|
|
|
|
| 78 |
print(f"Client removed from group '{group_name}'")
|
| 79 |
|
| 80 |
async def broadcast_to_group(self, message: str, group_name: str):
|
|
@@ -94,16 +95,20 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 94 |
data = await websocket.receive_text()
|
| 95 |
try:
|
| 96 |
message = json.loads(data)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
if websocket in manager.connections:
|
| 108 |
group_name = manager.connections[websocket]
|
| 109 |
await manager.broadcast_to_group(data, group_name)
|
|
@@ -113,29 +118,15 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 113 |
"message": "Join a group before sending messages"
|
| 114 |
})
|
| 115 |
await websocket.send_text(error)
|
|
|
|
| 116 |
except json.JSONDecodeError:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
except KeyError:
|
| 123 |
-
error = json.dumps({
|
| 124 |
-
"type": "error",
|
| 125 |
-
"message": "Missing required fields in message"
|
| 126 |
-
})
|
| 127 |
-
await websocket.send_text(error)
|
| 128 |
except WebSocketDisconnect:
|
| 129 |
-
# Get group before disconnecting
|
| 130 |
-
group_name = manager.connections.get(websocket)
|
| 131 |
manager.disconnect(websocket)
|
| 132 |
-
# Notify group about disconnection
|
| 133 |
-
if group_name:
|
| 134 |
-
notification = json.dumps({
|
| 135 |
-
"type": "system",
|
| 136 |
-
"message": "A member has left"
|
| 137 |
-
})
|
| 138 |
-
await manager.broadcast_to_group(notification, group_name)
|
| 139 |
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
|
|
|
| 11 |
# Configure CORS
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Serve static files
|
| 21 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 22 |
|
| 23 |
# Set up Jinja2 templates
|
|
|
|
| 74 |
if len(self.groups[group_name]) == 0:
|
| 75 |
del self.groups[group_name]
|
| 76 |
print(f"Group '{group_name}' deleted (no members)")
|
| 77 |
+
if websocket in self.connections:
|
| 78 |
+
del self.connections[websocket]
|
| 79 |
print(f"Client removed from group '{group_name}'")
|
| 80 |
|
| 81 |
async def broadcast_to_group(self, message: str, group_name: str):
|
|
|
|
| 95 |
data = await websocket.receive_text()
|
| 96 |
try:
|
| 97 |
message = json.loads(data)
|
| 98 |
+
# Handle join messages
|
| 99 |
+
if message.get('type') == 'join':
|
| 100 |
+
group_name = message.get('group')
|
| 101 |
+
if group_name:
|
| 102 |
+
await manager.join_group(websocket, group_name)
|
| 103 |
+
else:
|
| 104 |
+
error = json.dumps({
|
| 105 |
+
"type": "error",
|
| 106 |
+
"message": "Missing group name in join request"
|
| 107 |
+
})
|
| 108 |
+
await websocket.send_text(error)
|
| 109 |
+
|
| 110 |
+
# Broadcast all other messages to the group
|
| 111 |
+
else:
|
| 112 |
if websocket in manager.connections:
|
| 113 |
group_name = manager.connections[websocket]
|
| 114 |
await manager.broadcast_to_group(data, group_name)
|
|
|
|
| 118 |
"message": "Join a group before sending messages"
|
| 119 |
})
|
| 120 |
await websocket.send_text(error)
|
| 121 |
+
|
| 122 |
except json.JSONDecodeError:
|
| 123 |
+
# Handle non-JSON messages by broadcasting directly
|
| 124 |
+
if websocket in manager.connections:
|
| 125 |
+
group_name = manager.connections[websocket]
|
| 126 |
+
await manager.broadcast_to_group(data, group_name)
|
| 127 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
except WebSocketDisconnect:
|
|
|
|
|
|
|
| 129 |
manager.disconnect(websocket)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|