@@ -8,8 +8,8 @@ import collaborationRoutes from "./routes/collaborationRoutes";
8
8
import { errorHandler } from "./middlewares/errorHandler" ;
9
9
import { connectToDatabase } from "./utils/database" ;
10
10
import { setUpKafkaSubscribers } from "./utils/kafkaClient" ;
11
- import { YDocManager } from './utils/yjs' ;
12
- import * as Y from 'yjs' ;
11
+ const setupWSConnection = require ( "y-websocket/bin/utils" ) . setupWSConnection ;
12
+
13
13
14
14
dotenv . config ( { path : path . resolve ( __dirname , "./.env" ) } ) ;
15
15
@@ -36,40 +36,43 @@ app.use(errorHandler);
36
36
const wss = new WebSocket . Server ( { server } ) ;
37
37
38
38
wss . on ( "connection" , ( ws , req ) => {
39
- // Extract sessionId from the URL for connecting to the Yjs document
40
- const sessionId = req . url ?. split ( '/' ) . pop ( ) ;
39
+ // // Extract sessionId from the URL for connecting to the Yjs document
40
+ // const sessionId = req.url?.split('/').pop();
41
+
42
+ // console.log(`A user has connected to session: ${sessionId}!`)
41
43
42
- if ( sessionId ) {
43
- // Initialize or get the Yjs document from YDocManager
44
- const ydoc = YDocManager . getDoc ( sessionId ) || YDocManager . initializeDoc ( sessionId ) ;
44
+ // if (sessionId) {
45
+ // // Initialize or get the Yjs document from YDocManager
46
+ // const ydoc = YDocManager.getDoc(sessionId) || YDocManager.initializeDoc(sessionId);
45
47
46
- // Set up the document synchronization logic
47
- const encoder = Y . encodeStateAsUpdate ( ydoc ) ;
48
- ws . send ( encoder ) ;
48
+ // // Set up the document synchronization logic
49
+ // const encoder = Y.encodeStateAsUpdate(ydoc);
50
+ // ws.send(encoder);
49
51
50
- ws . on ( 'message' , ( message ) => {
51
- const update = new Uint8Array ( message instanceof ArrayBuffer ? message : ( message as Buffer ) . buffer ) ;
52
- Y . applyUpdate ( ydoc , update ) ;
53
- wss . clients . forEach ( ( client ) => {
54
- if ( client !== ws && client . readyState === WebSocket . OPEN ) {
55
- client . send ( update ) ;
56
- }
57
- } ) ;
58
- } ) ;
52
+ // ws.on('message', (message) => {
53
+ // const update = new Uint8Array(message instanceof ArrayBuffer ? message : (message as Buffer).buffer);
54
+ // Y.applyUpdate(ydoc, update);
55
+ // wss.clients.forEach((client) => {
56
+ // if (client !== ws && client.readyState === WebSocket.OPEN) {
57
+ // client.send(update);
58
+ // }
59
+ // });
60
+ // });
59
61
60
- ws . on ( 'close' , ( ) => {
61
- console . log ( `Connection closed for session: ${ sessionId } ` ) ;
62
- } ) ;
63
-
64
- } else {
65
- console . error ( "No session ID found in WebSocket connection URL." ) ;
66
- ws . close ( ) ;
67
- }
62
+ // ws.on('close', () => {
63
+ // console.log(`Connection closed for session: ${sessionId}`);
64
+ // });
65
+
66
+ // } else {
67
+ // console.error("No session ID found in WebSocket connection URL.");
68
+ // ws.close();
69
+ // }
70
+ setupWSConnection ( ws , req ) ;
68
71
} ) ;
69
72
70
73
// Start the server
71
74
server . listen ( port , async ( ) => {
72
75
console . log ( `Collaboration service running on http://localhost:${ port } ` ) ;
73
76
74
77
await setUpKafkaSubscribers ( ) ;
75
- } ) ;
78
+ } ) ;
0 commit comments