|
|
@@ -1,12 +1,15 @@
|
|
|
import { GlobalSocketEventName } from '@growi/core/dist/interfaces';
|
|
|
import { Server } from 'socket.io';
|
|
|
|
|
|
+import { CurrentPageYjsDraftData } from '~/interfaces/page';
|
|
|
+import { SocketEventName } from '~/interfaces/websocket';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
import { RoomPrefix, getRoomNameWithId } from '../util/socket-io-helpers';
|
|
|
|
|
|
import { getYjsConnectionManager } from './yjs-connection-manager';
|
|
|
|
|
|
+
|
|
|
const expressSession = require('express-session');
|
|
|
const passport = require('passport');
|
|
|
|
|
|
@@ -51,6 +54,7 @@ class SocketIoService {
|
|
|
|
|
|
await this.setupLoginedUserRoomsJoinOnConnection();
|
|
|
await this.setupDefaultSocketJoinRoomsEventHandler();
|
|
|
+ await this.setupDefaultSocketLeaveRoomsEventHandler();
|
|
|
}
|
|
|
|
|
|
getDefaultSocket() {
|
|
|
@@ -149,16 +153,30 @@ class SocketIoService {
|
|
|
setupDefaultSocketJoinRoomsEventHandler() {
|
|
|
this.io.on('connection', (socket) => {
|
|
|
// set event handlers for joining rooms
|
|
|
- socket.on('join:page', ({ pageId }) => {
|
|
|
+ socket.on(SocketEventName.JoinPage, ({ pageId }) => {
|
|
|
socket.join(getRoomNameWithId(RoomPrefix.PAGE, pageId));
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ setupDefaultSocketLeaveRoomsEventHandler() {
|
|
|
+ this.io.on('connection', (socket) => {
|
|
|
+ socket.on(SocketEventName.LeavePage, ({ pageId }) => {
|
|
|
+ socket.leave(getRoomNameWithId(RoomPrefix.PAGE, pageId));
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
setupYjsConnection() {
|
|
|
const yjsConnectionManager = getYjsConnectionManager();
|
|
|
this.io.on('connection', (socket) => {
|
|
|
socket.on(GlobalSocketEventName.YDocSync, async({ pageId, initialValue }) => {
|
|
|
+
|
|
|
+ // Emit to the client in the room of the target pageId.
|
|
|
+ this.io
|
|
|
+ .in(getRoomNameWithId(RoomPrefix.PAGE, pageId))
|
|
|
+ .emit(SocketEventName.YjsUpdated, CurrentPageYjsDraftData.hasYjsDraft);
|
|
|
+
|
|
|
try {
|
|
|
await yjsConnectionManager.handleYDocSync(pageId, initialValue);
|
|
|
}
|