Daniel Marques commited on
Commit
48c871b
1 Parent(s): 628c9d6

fix: convert json in string

Browse files
Files changed (1) hide show
  1. main.py +32 -2
main.py CHANGED
@@ -214,7 +214,7 @@ async def create_upload_file(file: UploadFile):
214
  return {"filename": file.filename}
215
 
216
  @api_app.websocket("/ws/{room_id}/{user_id}")
217
- async def websocket_endpoint(websocket: WebSocket, room_id: str):
218
  global QA
219
 
220
  await socket_manager.add_user_to_room(room_id, websocket)
@@ -237,9 +237,39 @@ async def websocket_endpoint(websocket: WebSocket, room_id: str):
237
  await socket_manager.remove_user_from_room(room_id, websocket)
238
 
239
  message = {
240
- "message": f"User {user_id} disconnected from room - {room_id}"
241
  }
242
 
243
  await socket_manager.broadcast_to_room(room_id, json.dumps(message))
244
  except RuntimeError as error:
245
  print(error)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  return {"filename": file.filename}
215
 
216
  @api_app.websocket("/ws/{room_id}/{user_id}")
217
+ async def websocket_endpoint_room(websocket: WebSocket, room_id: str, user_id: str):
218
  global QA
219
 
220
  await socket_manager.add_user_to_room(room_id, websocket)
 
237
  await socket_manager.remove_user_from_room(room_id, websocket)
238
 
239
  message = {
240
+ "message": f"Student {user_id} disconnected from room - {room_id}"
241
  }
242
 
243
  await socket_manager.broadcast_to_room(room_id, json.dumps(message))
244
  except RuntimeError as error:
245
  print(error)
246
+
247
+ @api_app.websocket("/ws/{user_id}")
248
+ async def websocket_endpoint_student(websocket: WebSocket, user_id: str):
249
+ global QA
250
+
251
+ message = {
252
+ "message": f"Student {user_id} connected"
253
+ }
254
+
255
+ await socket_manager.add_user_to_room(user_id, websocket)
256
+ await socket_manager.broadcast_to_room(user_id, json.dumps(message))
257
+
258
+ try:
259
+ while True:
260
+ data = await websocket.receive_text()
261
+
262
+ prompt_response_dict = sendPromptChain(QA, data)
263
+
264
+ await socket_manager.broadcast_to_room(user_id, json.dumps(prompt_response_dict))
265
+
266
+ except WebSocketDisconnect:
267
+ await socket_manager.remove_user_from_room(user_id, websocket)
268
+
269
+ message = {
270
+ "message": f"Student {user_id} disconnected"
271
+ }
272
+
273
+ await socket_manager.broadcast_to_room(user_id, json.dumps(message))
274
+ except RuntimeError as error:
275
+ print(error)