소스 검색

Checked user rooms function

Taichi Masuyama 4 년 전
부모
커밋
f14c8877d6

+ 5 - 0
packages/app/src/client/services/SocketIoContainer.js

@@ -32,6 +32,11 @@ export default class SocketIoContainer extends Container {
       logger.error(error);
     });
 
+    // DELETE THIS THIS IS FOR ONLY TESTING PURPOSE SO FAR
+    this.socket.on('in_app_notification', (data) => {
+      console.log('Received data: ', data);
+    });
+
     this.state = {
     };
 

+ 6 - 0
packages/app/src/server/routes/apiv3/page.js

@@ -132,6 +132,7 @@ module.exports = (crowi) => {
   const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
 
   const globalNotificationService = crowi.getGlobalNotificationService();
+  const socketIoService = crowi.socketIoService;
   const { Page, GlobalNotificationSetting } = crowi.models;
   const { exportService } = crowi;
 
@@ -187,6 +188,11 @@ module.exports = (crowi) => {
   router.put('/likes', accessTokenParser, loginRequiredStrictly, csrf, validator.likes, apiV3FormValidator, async(req, res) => {
     const { pageId, bool: isLiked } = req.body;
 
+    // DELETE THIS THIS IS FOR ONLY TESTING PURPOSE
+    await socketIoService.getDefaultSocket().in(`user:${req.user._id}`).emit('in_app_notification', {
+      testData: 'Test data',
+    });
+
     let page;
     try {
       page = await Page.findByIdAndViewer(pageId, req.user);

+ 17 - 0
packages/app/src/server/service/socket-io.js

@@ -40,6 +40,8 @@ class SocketIoService {
     await this.setupCheckConnectionLimitsMiddleware();
 
     await this.setupStoreGuestIdEventHandler();
+
+    await this.setupDefaultSocketRoomsEventHandler();
   }
 
   getDefaultSocket() {
@@ -124,6 +126,21 @@ class SocketIoService {
     });
   }
 
+  setupDefaultSocketRoomsEventHandler() {
+    this.io.on('connection', (socket) => {
+      // TODO: check if i can get page information here and use or not
+      // TODO: join page rooms here if possible
+      const user = socket.request.user;
+      if (user == null) {
+        logger.debug('Socket io: An anonymous user has connected');
+        return;
+      }
+      // make a room for each user. it leaves automatically
+      // TODO: avoid hard coding by using a utility function
+      socket.join(`user:${user._id}`);
+    });
+  }
+
   async checkConnectionLimitsForAdmin(socket, next) {
     const namespaceName = socket.nsp.name;