@@ -150,6 +150,17 @@ export class CanvasEngine {
150150 }
151151
152152 private connectWebSocket ( ) {
153+ if (
154+ this . socket &&
155+ ( this . socket . readyState === WebSocket . CONNECTING ||
156+ this . socket . readyState === WebSocket . OPEN )
157+ ) {
158+ console . log ( "Connection already exists, not creating a new one" ) ;
159+ return ;
160+ }
161+
162+ console . log ( `Connecting to WebSocket with sessionId: ${ this . token } ` ) ;
163+
153164 const url = `${ WS_URL } ?token=${ encodeURIComponent ( this . token ! ) } ` ;
154165 this . socket = new WebSocket ( url ) ;
155166
@@ -188,6 +199,30 @@ export class CanvasEngine {
188199 }
189200 break ;
190201
202+ case WsDataType . EXISTING_SHAPES :
203+ if ( Array . isArray ( data . message ) && data . message . length > 0 ) {
204+ const decryptedShapes = await Promise . all (
205+ data . message . map ( async ( shape ) => {
206+ if ( shape . message ) {
207+ const decrypted = await decryptData (
208+ shape . message ,
209+ this . encryptionKey !
210+ ) ;
211+ return JSON . parse ( decrypted ) ;
212+ }
213+ return null ;
214+ } )
215+ ) ;
216+ console . log ( "decryptedShapes = " , decryptedShapes ) ;
217+
218+ const validShapes = decryptedShapes . filter ( ( s ) => s !== null ) ;
219+ if ( validShapes . length > 0 ) {
220+ this . updateShapes ( validShapes ) ;
221+ this . notifyShapeCountChange ( ) ;
222+ }
223+ }
224+ break ;
225+
191226 case WsDataType . DRAW :
192227 case WsDataType . UPDATE :
193228 if ( data . userId !== this . userId && data . message ) {
@@ -197,6 +232,7 @@ export class CanvasEngine {
197232 ) ;
198233 const shape = JSON . parse ( decrypted ) ;
199234 this . updateShapes ( [ shape ] ) ;
235+ this . notifyShapeCountChange ( ) ;
200236 }
201237 break ;
202238
@@ -215,7 +251,7 @@ export class CanvasEngine {
215251 this . isConnected = false ;
216252 this . onConnectionChange ?.( false ) ;
217253 console . warn ( "WebSocket closed:" , e ) ;
218- setTimeout ( ( ) => this . connectWebSocket ( ) , 1000 ) ;
254+ setTimeout ( ( ) => this . connectWebSocket ( ) , 2000 ) ;
219255 } ;
220256
221257 this . socket . onerror = ( err ) => {
@@ -1383,7 +1419,8 @@ export class CanvasEngine {
13831419 ) ;
13841420
13851421 if ( rounded === "round" ) {
1386- const r = Math . min ( normalizedWidth , normalizedHeight ) * ROUND_RADIUS_FACTOR ;
1422+ const r =
1423+ Math . min ( normalizedWidth , normalizedHeight ) * ROUND_RADIUS_FACTOR ;
13871424
13881425 this . roughCanvas . path (
13891426 `M ${ posX + r } ${ posY }
0 commit comments