2
2
from fastapi import APIRouter , Query , WebSocket , WebSocketDisconnect
3
3
from app .core .connection_manager import connection_manager
4
4
from app .core .users import UserState
5
- from app .schemas .messages import Message , ConnectMessage , DisconnectMessage , DisplayMessage
5
+ from app .schemas .messages import Message , CollaboratorConnectMessage , CollaboratorDisconnectMessage , DisplayMessage
6
6
7
7
from datetime import datetime
8
8
import asyncio
@@ -36,19 +36,22 @@ async def receiver():
36
36
msg = await ws .receive_text ()
37
37
await connection_manager .on_message (session_id , user_id , msg )
38
38
except WebSocketDisconnect :
39
- print ("aandle disconnect" )
40
- pass ##
39
+ try :
40
+ print ("handle disconnect" )
41
+ await connection_manager .on_disconnect (session_id , user_id )
42
+ except ValueError as e :
43
+ print (f"Error handling disconnect: { e } " )
41
44
42
45
receiver_task = asyncio .create_task (receiver ())
43
46
44
47
try :
45
48
while True :
46
49
msg = await in_q .get ()
47
50
48
- if isinstance (msg , ConnectMessage ):
51
+ if isinstance (msg , CollaboratorConnectMessage ):
49
52
user_status = UserState .AWAIT_POLLING
50
53
await ws .send_text (f"Connected to session { session_id } as user { user_id } . You can start collaborating!" )
51
- elif isinstance (msg , DisconnectMessage ):
54
+ elif isinstance (msg , CollaboratorDisconnectMessage ):
52
55
user_status = UserState .AWAIT_CONNECT
53
56
await ws .send_text (f"Collaborator disconnected, awaiting reconnection..." )
54
57
elif isinstance (msg , DisplayMessage ):
@@ -58,12 +61,12 @@ async def receiver():
58
61
59
62
await asyncio .sleep (0.1 )
60
63
61
- except WebSocketDisconnect as e :
62
- try :
63
- print ("handle disconnect" )
64
- await connection_manager .on_disconnect (session_id , user_id )
65
- except ValueError as e :
66
- print (f"Error handling disconnect: { e } " )
64
+ # except WebSocketDisconnect as e:
65
+ # try:
66
+ # print("handle disconnect")
67
+ # await connection_manager.on_disconnect(session_id, user_id)
68
+ # except ValueError as e:
69
+ # print(f"Error handling disconnect: {e}")
67
70
finally :
68
71
receiver_task .cancel ()
69
72
0 commit comments