浏览代码

update sync action

gemercheung 3 年之前
父节点
当前提交
a52c774865
共有 4 个文件被更改,包括 33 次插入9 次删除
  1. 14 1
      demo/client1.html
  2. 5 5
      src/controller/roomController.js
  3. 13 2
      src/controller/syncActionController.js
  4. 1 1
      src/core/logger.js

+ 14 - 1
demo/client1.html

@@ -13,6 +13,10 @@
 
   <body>
     <iframe src="./index.html"></iframe>
+
+    <button id="changeVoiceStatusOpen">开MIC</button>
+    <button id="changeVoiceStatusClose">关MIC</button>
+    <!-- <button id="stopCall">stopCall结束</button> -->
     </div>
 
     <script>
@@ -33,11 +37,20 @@
           myHeadUrl: "http://downza.img.zz314.com/edu/pc/wlgj-1008/2016-06-23/64ec0888b15773e3ba5b5f744b9df16c.jpg",
         },
       });
-
+    
       socket.on("connect", function (data) {
         console.log(data);
       });
 
+      var changeVoiceStatusOpen = document.querySelector("#changeVoiceStatusOpen");
+      changeVoiceStatusOpen.addEventListener("click", function () {
+        socket.emit("changeVoiceStatus",{status:true});
+      });
+      var changeVoiceStatusClose = document.querySelector("#changeVoiceStatusClose");
+      changeVoiceStatusClose.addEventListener("click", function () {
+        socket.emit("changeVoiceStatus",{status:false});
+      });
+
     
       
     </script>

+ 5 - 5
src/controller/roomController.js

@@ -20,7 +20,7 @@ const isHoster = (role) => {
 const buildRoom = async (roomId, userId, user) => {
   const hasJoin = await pubClient.HVALS(roomId, userId);
   if (hasJoin.length === 0) {
-    log.info("创建房间", { roomId });
+    logger.info("buildRoom", { roomId });
     await pubClient.hSet(roomId, userId, JSON.stringify(user));
   }
 };
@@ -31,7 +31,7 @@ const buildRoom = async (roomId, userId, user) => {
  */
 
 const removeRoom = async (roomId) => {
-  log.info("removeRoom", { roomId });
+  logger.info("removeRoom", { roomId });
   await pubClient.del(roomId);
 };
 
@@ -42,7 +42,7 @@ const removeRoom = async (roomId) => {
  * @param {*} user
  */
 const joinRoom = async (roomId, userId, user) => {
-  log.info("joinRoom", { roomId });
+  logger.info("joinRoom", { roomId });
   const hasRoom = await pubClient.exists(roomId);
   if (hasRoom) {
     await pubClient.hSet(roomId, userId, JSON.stringify(user));
@@ -56,7 +56,7 @@ const joinRoom = async (roomId, userId, user) => {
  */
 
 const leaveRoom = async (roomId, userId) => {
-  console.log("leaveRoom", roomId);
+  logger.info("leaveRoom", roomId);
   await pubClient.hDel(roomId, userId);
 };
 
@@ -111,7 +111,7 @@ export async function roomController(socket) {
     if ("roomId" in user && "userId" in user) {
       user.isLogin = true;
       await pubClient.hSet(userUniqueId, user);
-      await pubClient.expire(userUniqueId, 60 * 60 * 1);
+      await pubClient.expire(userUniqueId, 60 * 60 * 24);
       // 房主自动创建房间
 
       if (isHoster(role) && Number(from) === 0) {

+ 13 - 2
src/controller/syncActionController.js

@@ -1,5 +1,6 @@
 import { logger } from "../core/logger.js";
 import { pubClient } from "../connection/redis.js";
+import { ROLES, CODEMEG, EVENT } from "../enum/index.js";
 
 const joinSyncClient = async (syncId, userId, from, socket) => {
   //   socket.join(syncId);
@@ -19,9 +20,19 @@ export async function syncActionController(socket) {
   const syncId = `sync-${userId}${roomId}`;
   if (user) {
     if ("roomId" in user && "userId" in user) {
-      //   await pubClient.set("test");
-      console.log("from-sync", from);
       joinSyncClient(syncId, userId, from, socket);
     }
+    socket.on(EVENT.clientSyncAction, (data) => {
+      //console.log(data, 'clientSyncAction')
+      socket.broadcast.to(syncId).emit(EVENT.clientSyncAction, data);
+    });
+
+    socket.on(EVENT.changeVoiceStatus, (data) => {
+      socket.broadcast.to(syncId).emit(EVENT.changeVoiceStatus, data);
+      logger.info("changeVoiceStatus", { data, syncId });
+      //   socket.broadcast.to(roomId).emit(EVENT.changeVoiceStatus, {
+      //     roomsPerson: sortRoomUser(_roomPerson),
+      //   });
+    });
   }
 }

+ 1 - 1
src/core/logger.js

@@ -22,7 +22,7 @@ const logger = createLogger({
 
   format: format.combine(
     format.colorize(),
-    format.timestamp({ format: "MMM D, YYYY HH:mm:ss" }),
+    format.timestamp({ format: "MMM D, YYYY HH:mm:ss.SSS" }),
     customFormat,
     // Do not use splat format
   ),