@@ -150,6 +150,17 @@ export class CanvasEngine {
150
150
}
151
151
152
152
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
+
153
164
const url = `${ WS_URL } ?token=${ encodeURIComponent ( this . token ! ) } ` ;
154
165
this . socket = new WebSocket ( url ) ;
155
166
@@ -188,6 +199,30 @@ export class CanvasEngine {
188
199
}
189
200
break ;
190
201
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
+
191
226
case WsDataType . DRAW :
192
227
case WsDataType . UPDATE :
193
228
if ( data . userId !== this . userId && data . message ) {
@@ -197,6 +232,7 @@ export class CanvasEngine {
197
232
) ;
198
233
const shape = JSON . parse ( decrypted ) ;
199
234
this . updateShapes ( [ shape ] ) ;
235
+ this . notifyShapeCountChange ( ) ;
200
236
}
201
237
break ;
202
238
@@ -215,7 +251,7 @@ export class CanvasEngine {
215
251
this . isConnected = false ;
216
252
this . onConnectionChange ?.( false ) ;
217
253
console . warn ( "WebSocket closed:" , e ) ;
218
- setTimeout ( ( ) => this . connectWebSocket ( ) , 1000 ) ;
254
+ setTimeout ( ( ) => this . connectWebSocket ( ) , 2000 ) ;
219
255
} ;
220
256
221
257
this . socket . onerror = ( err ) => {
@@ -1383,7 +1419,8 @@ export class CanvasEngine {
1383
1419
) ;
1384
1420
1385
1421
if ( rounded === "round" ) {
1386
- const r = Math . min ( normalizedWidth , normalizedHeight ) * ROUND_RADIUS_FACTOR ;
1422
+ const r =
1423
+ Math . min ( normalizedWidth , normalizedHeight ) * ROUND_RADIUS_FACTOR ;
1387
1424
1388
1425
this . roughCanvas . path (
1389
1426
`M ${ posX + r } ${ posY }
0 commit comments