Spaces:
Paused
Paused
ChandimaPrabath
commited on
Commit
•
6deb284
1
Parent(s):
bd7e564
no ws
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException,
|
2 |
from pydantic import BaseModel
|
3 |
from typing import Dict, List
|
4 |
import bcrypt
|
@@ -36,7 +36,6 @@ def generate_rsa_key_pair():
|
|
36 |
key_size=2048
|
37 |
)
|
38 |
public_key = private_key.public_key()
|
39 |
-
# Serialize the private key with encryption (optional)
|
40 |
private_key_pem = private_key.private_bytes(
|
41 |
encoding=serialization.Encoding.PEM,
|
42 |
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
@@ -125,39 +124,8 @@ async def delete_message(recipient: str, message: str):
|
|
125 |
logger.info(f"Message deleted from {recipient}: {message}")
|
126 |
return JSONResponse(content={"status": "Message deleted"})
|
127 |
|
128 |
-
@app.
|
129 |
-
async def
|
130 |
-
if username not in
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
await websocket.accept()
|
135 |
-
try:
|
136 |
-
while True:
|
137 |
-
data = await websocket.receive_text()
|
138 |
-
message_data = json.loads(data)
|
139 |
-
event_type = message_data.get("type")
|
140 |
-
recipient = message_data.get("recipient")
|
141 |
-
message = message_data.get("message")
|
142 |
-
|
143 |
-
if event_type == "send_message":
|
144 |
-
if recipient in messages:
|
145 |
-
messages[recipient].append(message)
|
146 |
-
else:
|
147 |
-
messages[recipient] = [message]
|
148 |
-
|
149 |
-
await websocket.send_text(json.dumps({"event": "message_received", "recipient": recipient, "message": message}))
|
150 |
-
|
151 |
-
elif event_type == "get_messages":
|
152 |
-
if username in messages:
|
153 |
-
await websocket.send_text(json.dumps({"event": "messages_list", "messages": messages[username]}))
|
154 |
-
else:
|
155 |
-
await websocket.send_text(json.dumps({"event": "messages_list", "messages": []}))
|
156 |
-
|
157 |
-
else:
|
158 |
-
await websocket.send_text(json.dumps({"event": "error", "message": "Unknown event type"}))
|
159 |
-
except WebSocketDisconnect:
|
160 |
-
logger.info(f"WebSocket disconnected: {username}")
|
161 |
-
except Exception as e:
|
162 |
-
logger.error(f"Error in WebSocket communication: {e}")
|
163 |
-
await websocket.close(code=1011) # Internal server error
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Depends
|
2 |
from pydantic import BaseModel
|
3 |
from typing import Dict, List
|
4 |
import bcrypt
|
|
|
36 |
key_size=2048
|
37 |
)
|
38 |
public_key = private_key.public_key()
|
|
|
39 |
private_key_pem = private_key.private_bytes(
|
40 |
encoding=serialization.Encoding.PEM,
|
41 |
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
|
|
124 |
logger.info(f"Message deleted from {recipient}: {message}")
|
125 |
return JSONResponse(content={"status": "Message deleted"})
|
126 |
|
127 |
+
@app.get("/poll_messages/{username}")
|
128 |
+
async def poll_messages(username: str):
|
129 |
+
if username not in messages:
|
130 |
+
return JSONResponse(content={"messages": []})
|
131 |
+
return JSONResponse(content={"messages": messages[username]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|