@@ -44,6 +44,11 @@ import type { Point } from "roughjs/bin/geometry";
44
44
import { getFontSize , getLineHeight } from "@/utils/textUtils" ;
45
45
import { generateFreeDrawPath } from "./RenderElements" ;
46
46
47
+ type WebSocketConnection = {
48
+ connectionId : string ;
49
+ connected : boolean ;
50
+ } ;
51
+
47
52
export class CanvasEngine {
48
53
private canvas : HTMLCanvasElement ;
49
54
private ctx : CanvasRenderingContext2D ;
@@ -96,6 +101,9 @@ export class CanvasEngine {
96
101
97
102
private roughSeed : number = 1 ;
98
103
104
+ private connectionId : string | null = null ;
105
+ private myConnections : WebSocketConnection [ ] = [ ] ;
106
+
99
107
constructor (
100
108
canvas : HTMLCanvasElement ,
101
109
roomId : string | null ,
@@ -180,17 +188,35 @@ export class CanvasEngine {
180
188
this . socket . onmessage = async ( event ) => {
181
189
try {
182
190
const data : WebSocketMessage = JSON . parse ( event . data ) ;
191
+ if ( data . type === WsDataType . CONNECTION_READY ) {
192
+ this . connectionId = data . connectionId ;
193
+ console . log ( `Assigned connection ID: ${ this . connectionId } ` ) ;
194
+ }
183
195
184
196
switch ( data . type ) {
185
197
case WsDataType . USER_JOINED :
198
+ if (
199
+ data . userId === this . userId &&
200
+ data . connectionId !== this . connectionId
201
+ ) {
202
+ this . myConnections . push ( {
203
+ connectionId : data . connectionId ,
204
+ connected : true ,
205
+ } ) ;
206
+ console . log ( `🔁 Another tab detected: ${ data . connectionId } ` ) ;
207
+ }
186
208
if ( data . participants && Array . isArray ( data . participants ) ) {
187
209
this . participants = data . participants ;
188
210
this . onParticipantsUpdate ?.( this . participants ) ;
189
211
}
190
-
191
212
break ;
192
213
193
214
case WsDataType . USER_LEFT :
215
+ if ( data . userId === this . userId && data . connectionId ) {
216
+ this . myConnections = this . myConnections . filter (
217
+ ( c ) => c . connectionId !== data . connectionId
218
+ ) ;
219
+ }
194
220
if ( data . userId ) {
195
221
this . participants = this . participants . filter (
196
222
( u ) => u . userId !== data . userId
@@ -225,7 +251,20 @@ export class CanvasEngine {
225
251
226
252
case WsDataType . DRAW :
227
253
case WsDataType . UPDATE :
228
- if ( data . userId !== this . userId && data . message ) {
254
+ if (
255
+ data . userId === this . userId &&
256
+ data . connectionId !== this . connectionId
257
+ ) {
258
+ if ( data . message ) {
259
+ const decrypted = await decryptData (
260
+ data . message ,
261
+ this . encryptionKey !
262
+ ) ;
263
+ const shape = JSON . parse ( decrypted ) ;
264
+ this . updateShapes ( [ shape ] ) ;
265
+ this . notifyShapeCountChange ( ) ;
266
+ }
267
+ } else if ( data . userId !== this . userId && data . message ) {
229
268
const decrypted = await decryptData (
230
269
data . message ,
231
270
this . encryptionKey !
@@ -237,7 +276,14 @@ export class CanvasEngine {
237
276
break ;
238
277
239
278
case WsDataType . ERASER :
240
- if ( data . userId !== this . userId && data . id ) {
279
+ if (
280
+ data . userId === this . userId &&
281
+ data . connectionId !== this . connectionId
282
+ ) {
283
+ if ( data . id ) {
284
+ this . removeShape ( data . id ) ;
285
+ }
286
+ } else if ( data . userId !== this . userId && data . id ) {
241
287
this . removeShape ( data . id ) ;
242
288
}
243
289
break ;
@@ -306,6 +352,7 @@ export class CanvasEngine {
306
352
userName : this . userName ! ,
307
353
timestamp : new Date ( ) . toISOString ( ) ,
308
354
participants : null ,
355
+ connectionId : this . connectionId ! ,
309
356
} ) ;
310
357
}
311
358
}
@@ -628,6 +675,7 @@ export class CanvasEngine {
628
675
userName : this . userName ! ,
629
676
timestamp : new Date ( ) . toISOString ( ) ,
630
677
participants : null ,
678
+ connectionId : this . connectionId ! ,
631
679
} ) ;
632
680
console . error ( "Error sending shape update ws message" , e ) ;
633
681
}
@@ -795,6 +843,7 @@ export class CanvasEngine {
795
843
type : WsDataType . UPDATE ,
796
844
id : shape . id ,
797
845
message : JSON . stringify ( shape ) ,
846
+ connectionId : this . connectionId ! ,
798
847
roomId : this . roomId ,
799
848
userId : this . userId ! ,
800
849
userName : this . userName ! ,
@@ -1848,6 +1897,7 @@ export class CanvasEngine {
1848
1897
userName : this . userName ! ,
1849
1898
timestamp : new Date ( ) . toISOString ( ) ,
1850
1899
participants : null ,
1900
+ connectionId : this . connectionId ! ,
1851
1901
} ) ;
1852
1902
console . error ( "Error sending shape update ws message" , e ) ;
1853
1903
}
0 commit comments