gemercheung 3 år sedan
förälder
incheckning
b1568f0fbe

+ 9 - 10
config.yaml

@@ -5,7 +5,6 @@ http:
   host: '0.0.0.0'
   host: '0.0.0.0'
   port: 6688
   port: 6688
 
 
-
 # grpc:
 # grpc:
 #   url: '221.4.210.172:23000'
 #   url: '221.4.210.172:23000'
 grpc:
 grpc:
@@ -25,23 +24,23 @@ redis:
   decode_responses: true
   decode_responses: true
   db: 9
   db: 9
 
 
-queueRedis:
-  port: 6379
-  host: 'localhost' #远程调试需要设置bindip 为0.0.0.0 并且设置密码
-  password: 'redis9394' # 非远程不需要密码
-  decode_responses: true
-  db: 14
+
+
+# queueRedis:
+#   port: 6379
+#   host: 'localhost' #远程调试需要设置bindip 为0.0.0.0 并且设置密码
+#   password: 'redis9394' # 非远程不需要密码
+#   decode_responses: true
+#   db: 14
 
 
 stun:
 stun:
-  server: ['stun:172.18.156.41:3478', 'stun:120.24.252.95:3478']
+  server: ['stun:120.24.252.95:3478', 'stun:172.18.156.41:3478']
   portRangeBegin: 52000
   portRangeBegin: 52000
   portRangeEnd: 53000
   portRangeEnd: 53000
 
 
 server:
 server:
   private_ip: 172.18.197.114
   private_ip: 172.18.197.114
   public_ip: 120.24.252.95
   public_ip: 120.24.252.95
-
-  
 # PRIVATE_IP=172.18.197.114
 # PRIVATE_IP=172.18.197.114
 # PUBLIC_IP=120.24.252.95
 # PUBLIC_IP=120.24.252.95
 # STUNS_SEVER="stun:172.18.156.41:3478,stun:120.24.252.95:3478"
 # STUNS_SEVER="stun:172.18.156.41:3478,stun:120.24.252.95:3478"

+ 74 - 43
src/get-router/get-router.service.ts

@@ -1,29 +1,53 @@
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
+import { readFileSync } from 'fs';
+import { join } from 'path';
 import { CacheService } from 'src/cache/cache.service';
 import { CacheService } from 'src/cache/cache.service';
 
 
 @Injectable()
 @Injectable()
-export class GetRouterService {
+export class GetRouterService implements OnModuleInit {
   constructor(private cacheService: CacheService) {}
   constructor(private cacheService: CacheService) {}
+  private breakPointInfo: any;
+  private logger: Logger = new Logger('GetRouterService');
+
+  async onModuleInit() {
+    try {
+      const path = join(__dirname, '../ws/points-BreakPointId.json');
+      // const path = join(__dirname, '../ws/demo1.json');
+      const data = await readFileSync(path);
+      const BreakPointInfo = JSON.parse(Buffer.from(data).toString('utf-8'));
+      this.breakPointInfo = BreakPointInfo;
+
+      // console.log('BreakPointInfo', BreakPointInfo);
+    } catch (error) {
+      this.logger.error('load-json-error', error);
+    }
+  }
 
 
-  async searchRoad(appId, startPointId, clicking_point) {
+  searchRoad(appId, startPointId, clicking_point) {
     //表示终点
     //表示终点
     let endPoint;
     let endPoint;
 
 
-    const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
+    //const keys = this.cacheService.keys(`breakpoints:app_id:${appId}*`);
     let minDis = null;
     let minDis = null;
-    for (let i = 0; i < keys.length; ++i) {
-      const breakPointRes = await this.cacheService.get(keys[i]);
-      const breakPoint = JSON.parse(breakPointRes);
-      if (breakPoint.breakPointId == startPointId) {
+    //for (let i = 0; i < keys.length; ++i) {
+    for (const key in this.breakPointInfo) {
+      //const breakPointRes = await this.cacheService.get(keys[i]);
+      //const breakPoint = JSON.parse(breakPointRes);
+      if (Number(key) == startPointId) {
         continue;
         continue;
       }
       }
-      const position = breakPoint.position;
+      //const position = breakPoint.position;
+      const breakPoint = this.breakPointInfo[key];
       if (minDis == null) {
       if (minDis == null) {
-        minDis = this.getDistance(clicking_point, position);
+        minDis = this.getDistance(clicking_point, breakPoint.position);
         endPoint = breakPoint;
         endPoint = breakPoint;
-      } else if (minDis > this.getDistance(clicking_point, position)) {
+        endPoint.breakPointId = Number(key);
+      } else if (
+        minDis > this.getDistance(clicking_point, breakPoint.position)
+      ) {
         endPoint = breakPoint;
         endPoint = breakPoint;
-        minDis = this.getDistance(clicking_point, position);
+        minDis = this.getDistance(clicking_point, breakPoint.position);
+        endPoint.breakPointId = Number(key);
       }
       }
     }
     }
 
 
@@ -31,11 +55,11 @@ export class GetRouterService {
       return [];
       return [];
     }
     }
 
 
-    const startPointRes = await this.cacheService.get(
-      'breakpoints:app_id:' + appId + ':break_point_id:' + startPointId,
-    );
-    const startPoint = JSON.parse(startPointRes);
-
+    // const startPointRes = await this.cacheService.get(
+    //   'breakpoints:app_id:' + appId + ':break_point_id:' + startPointId,
+    // );
+    // const startPoint = JSON.parse(startPointRes);
+    const startPoint = this.breakPointInfo[startPointId];
     // const endPointRes = await this.cacheService.get(
     // const endPointRes = await this.cacheService.get(
     //   'breakpoints:app_id:' + appId + ':break_point_id:' + endPointId,
     //   'breakpoints:app_id:' + appId + ':break_point_id:' + endPointId,
     // );
     // );
@@ -47,39 +71,46 @@ export class GetRouterService {
     let result_index: number; //结果数组在开启列表中的序号
     let result_index: number; //结果数组在开启列表中的序号
 
 
     //openList.push({x:startPoint.x,y:startPoint.y,G:0});//把当前点加入到开启列表中,并且G是0
     //openList.push({x:startPoint.x,y:startPoint.y,G:0});//把当前点加入到开启列表中,并且G是0
-    openList.push({ breakPointId: startPointId, G: 0 }); //把当前点加入到开启列表中,并且G是0
+    openList.push({
+      breakPointId: startPointId,
+      G: 0,
+      position: startPoint.position,
+      contact: startPoint.contact,
+    }); //把当前点加入到开启列表中,并且G是0
 
 
     do {
     do {
-      const currentPointInfo = openList.pop();
-      const currentPointRes = await this.cacheService.get(
-        'breakpoints:app_id:' +
-          appId +
-          ':break_point_id:' +
-          currentPointInfo.breakPointId,
-      );
-      const currentPoint = JSON.parse(currentPointRes);
-      closeList.push(currentPointInfo);
+      const currentPoint = openList.pop();
+      // const currentPointId = currentPointInfo.breakPointId;
+      // const currentPoint = {
+      //   breakPointId: currentPointInfo.breakPointId,
+      //   position: this.breakPointInfo[currentPointId].position,
+      //   contact:this.breakPointInfo[currentPointId].contact,
+      //   G: currentPointInfo.G,
+      // };
+      closeList.push(currentPoint);
 
 
       //读redis里的数据
       //读redis里的数据
-      const breakPointRes = await this.cacheService.get(
-        'breakpoints:app_id:' +
-          appId +
-          ':break_point_id:' +
-          currentPointInfo.breakPointId,
-      );
+      // const breakPointRes = await this.cacheService.get(
+      //   'breakpoints:app_id:' +
+      //     appId +
+      //     ':break_point_id:' +
+      //     currentPointInfo.breakPointId,
+      // );
       let surroundPoint = [];
       let surroundPoint = [];
-      const _breakPoint = JSON.parse(breakPointRes);
+      //const _breakPoint = JSON.parse(breakPointRes);
+      const _breakPoint = this.breakPointInfo[currentPoint.breakPointId];
       surroundPoint = _breakPoint.contact;
       surroundPoint = _breakPoint.contact;
-
       for (let i = 0; i < surroundPoint.length; ++i) {
       for (let i = 0; i < surroundPoint.length; ++i) {
         const neighPointId = surroundPoint[i];
         const neighPointId = surroundPoint[i];
-        const itemRes = await this.cacheService.get(
-          'breakpoints:app_id:' + appId + ':break_point_id:' + neighPointId,
-        );
-        const item = JSON.parse(itemRes);
+        // const itemRes = await this.cacheService.get(
+        //   'breakpoints:app_id:' + appId + ':break_point_id:' + neighPointId,
+        // );
+        // const item = JSON.parse(itemRes);
+        const item = this.breakPointInfo[neighPointId];
+        item.breakPointId = neighPointId;
         //g 到父节点的位置
         //g 到父节点的位置
         const g =
         const g =
-          currentPointInfo.G +
+          currentPoint.G +
           this.getDistance(currentPoint.position, item.position);
           this.getDistance(currentPoint.position, item.position);
         if (this.existList(item, openList) == -1) {
         if (this.existList(item, openList) == -1) {
           //如果不在开启列表中
           //如果不在开启列表中
@@ -116,10 +147,10 @@ export class GetRouterService {
         //把路劲节点添加到result当中
         //把路劲节点添加到result当中
         result.unshift(currentObj.breakPointId);
         result.unshift(currentObj.breakPointId);
         currentObj = currentObj.parent;
         currentObj = currentObj.parent;
-        // debugger
       } while (
       } while (
-        currentObj.position.x != startPoint.position.x ||
-        currentObj.position.y != startPoint.position.y
+        //currentObj.position.x != startPoint.position.x ||
+        //currentObj.position.y != startPoint.position.y
+        currentObj.contact.indexOf(startPointId) < 0
       );
       );
     }
     }
     result.unshift(startPointId);
     result.unshift(startPointId);

+ 41 - 12
src/move/move.service.ts

@@ -1,14 +1,19 @@
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
+import { readFileSync } from 'fs';
+import { join } from 'path';
 import { CacheService } from 'src/cache/cache.service';
 import { CacheService } from 'src/cache/cache.service';
 import { RotateService } from 'src/rotate/rotate.service';
 import { RotateService } from 'src/rotate/rotate.service';
+
+// import * as  BreakPointIds from '../../ws/points-BreakPointId.json';
 // import { SceneService } from 'src/scene/scene.service';
 // import { SceneService } from 'src/scene/scene.service';
 
 
 @Injectable()
 @Injectable()
-export class MoveService {
+export class MoveService implements OnModuleInit {
   constructor(
   constructor(
     private cacheService: CacheService,
     private cacheService: CacheService,
     private rotateService: RotateService,
     private rotateService: RotateService,
   ) {}
   ) {}
+  private logger: Logger = new Logger('MoveService');
   private Actions = {
   private Actions = {
     Clicking: 1,
     Clicking: 1,
     Rotation: 1014,
     Rotation: 1014,
@@ -101,6 +106,21 @@ export class MoveService {
     code: 0,
     code: 0,
     msg: 'OK',
     msg: 'OK',
   };
   };
+  private breakPointInfo: any;
+
+  async onModuleInit() {
+    try {
+      const path = join(__dirname, '../ws/points-BreakPointId.json');
+      // const path = join(__dirname, '../ws/demo1.json');
+      const data = await readFileSync(path);
+      const BreakPointInfo = JSON.parse(Buffer.from(data).toString('utf-8'));
+      this.breakPointInfo = BreakPointInfo;
+
+      // console.log('BreakPointInfo', BreakPointInfo);
+    } catch (error) {
+      this.logger.error('load-json-error', error);
+    }
+  }
 
 
   init(app_id, userId) {
   init(app_id, userId) {
     const user = {
     const user = {
@@ -154,7 +174,7 @@ export class MoveService {
       const traceIds = [];
       const traceIds = [];
       traceIds.push(traceId);
       traceIds.push(traceId);
 
 
-      debugger;
+      // debugger;
       //纠正,旋转传到缓存里
       //纠正,旋转传到缓存里
       const checkReplys = [];
       const checkReplys = [];
       // console.log('矫正: ' + angle + ' 度');
       // console.log('矫正: ' + angle + ' 度');
@@ -201,6 +221,7 @@ export class MoveService {
       this.reply['actionResponses'][0].traceId = traceId;
       this.reply['actionResponses'][0].traceId = traceId;
       console.log('矫正后-相机:' + user.camera.angle.yaw);
       console.log('矫正后-相机:' + user.camera.angle.yaw);
       const index = Math.round(user.camera.angle.yaw / 44); //过渡需要
       const index = Math.round(user.camera.angle.yaw / 44); //过渡需要
+      debugger;
       for (let i = 0; i < path.length - 1; ++i) {
       for (let i = 0; i < path.length - 1; ++i) {
         let pathReplys = [];
         let pathReplys = [];
         const start_break_point_id = path[i];
         const start_break_point_id = path[i];
@@ -345,7 +366,7 @@ export class MoveService {
     user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
     user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
   }
   }
 
 
-  async getBreakPoints(actionRequest) {
+  getBreakPoints(actionRequest) {
     const userId = actionRequest['user_id'];
     const userId = actionRequest['user_id'];
     const traceId = actionRequest['trace_id'];
     const traceId = actionRequest['trace_id'];
     const actionType = actionRequest['action_type'];
     const actionType = actionRequest['action_type'];
@@ -373,17 +394,25 @@ export class MoveService {
 
 
     //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
     //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
     //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
     //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
-    const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
-
-    for (let i = 0; i < keys.length; ++i) {
-      const breakPointRes = await this.cacheService.get(keys[i]);
-      const breakPoint = JSON.parse(breakPointRes);
-      const position = breakPoint.position;
+    //const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
+    for (const key in this.breakPointInfo) {
+      const breakPoint = this.breakPointInfo[key];
+      //const breakPoint = JSON.parse(breakPointRes);
+      //const position = breakPoint.position;
       reply['nps'].push({
       reply['nps'].push({
-        position: position,
-        breakPointId: breakPoint.breakPointId,
+        position: breakPoint.position,
+        breakPointId: Number(key),
       });
       });
     }
     }
+    // for (let i = 0; i < keys.length; ++i) {
+    //   const breakPointRes = await this.cacheService.get(keys[i]);
+    //   const breakPoint = JSON.parse(breakPointRes);
+    //   const position = breakPoint.position;
+    //   reply['nps'].push({
+    //     position: position,
+    //     breakPointId: breakPoint.breakPointId,
+    //   });
+    // }
     return reply;
     return reply;
   }
   }
 
 

+ 132 - 152
src/rotate/rotate.service.ts

@@ -144,171 +144,151 @@ export class RotateService {
     return reply;
     return reply;
   }
   }
 
 
-  async rotate(actionRequest) {
-    try {
-      const userId = actionRequest['user_id'];
-      if (this.actionRequestPool[userId]) {
-        this.actionRequestPool[userId].push(actionRequest);
-      } else {
-        this.actionRequestPool[userId] = [];
-        this.actionRequestPool[userId].push(actionRequest);
-      }
-
-      let reply = this.replies[userId];
-
-      const actionRequests = this.actionRequestPool[userId];
-      const user = this.users[userId];
-      // debugger;
-      let horizontal_move = user.rotateInfo.horizontal_move;
-      //const traceIds = user.traceIds;
-      let sub = 0;
-      for (let i = 0; i < actionRequests.length; ++i) {
-        if (actionRequests[i].action_type == this.Actions.Rotation) {
-          horizontal_move += actionRequests[i].rotation_action.horizontal_move;
-          reply.traceIds.push(actionRequests[i].trace_id);
-          const actionResponse = this.createActionResponse(
-            actionRequests[i].action_type,
-            actionRequests[i].trace_id,
-          );
-          reply.actionResponses.push(actionResponse);
-          ++sub;
+  //旋转请求
+  async rotate(actionRequest): Promise<any> {
+    return new Promise(async (resolve, reject) => {
+      try {
+        const userId = actionRequest['user_id'];
+        if (this.actionRequestPool[userId]) {
+          this.actionRequestPool[userId].push(actionRequest);
         } else {
         } else {
-          break;
+          this.actionRequestPool[userId] = [];
+          this.actionRequestPool[userId].push(actionRequest);
         }
         }
-      }
-
-      actionRequests.splice(0, sub);
-      const hAngle = horizontal_move * 90;
-      if (Math.abs(hAngle) < 1) {
-        user.rotateInfo.horizontal_move = horizontal_move;
-        //user.traceIds = traceIds;
-        this.replies[userId] = reply;
-        return null;
-      }
-      reply = await this.rotateForAngle(userId, Math.floor(hAngle));
-      return reply;
-      /*
-      user.rotateInfo.frameIndex += Math.floor(hAngle);
-      if (user.rotateInfo.frameIndex < 0) {
-        user.rotateInfo.frameIndex = 360 - user.rotateInfo.frameIndex;
-      } else if (user.rotateInfo.frameIndex > 359) {
-        user.rotateInfo.frameIndex -= 360;
-      }
-      reply['traceIds'] = traceIds;
-      reply['newUserStates'][0]['userId'] = userId;
-      //从redis里取
-      //let key = user.appId + "-"+user.breakPointId+"-"+user.rotateInfo.frameIndex;
-      const key =
-        'rotateframe:app_id:' +
-        user.appId +
-        ':frame_index:' +
-        user.rotateInfo.frameIndex +
-        ':break_point_id:' +
-        user.breakPointId;
-      // const value = null;
-
-      const redisData = await this.cacheService.get(key);
-      const value = JSON.parse(redisData);
-      console.log('redis', value);
-      user.camera['position'] = value ? value.cameraPosition : '';
-      user.camera['angle'] = value ? value.cameraAngle : '';
 
 
-      //this.reply['newUserStates'][0]['playerState'] .player
-      reply['newUserStates'][0]['playerState'].camera.position =
-        user.camera['position'];
-      reply['newUserStates'][0]['playerState'].camera.angle =
-        user.camera['angle'];
-      reply['newUserStates'][0]['playerState'].cameraCenter =
-        user.player.position;
-      // debugger
-      reply.mediaSrc =
-        '/' +
-        '0000000001' +
-        '/' +
-        user.breakPointId +
-        '/' +
-        value.directory +
-        '/' +
-        value.fileName +
-        '?m=' +
-        new Date().getTime();
+        let reply = this.replies[userId];
 
 
-      this.replies[userId].traceIds = [];
-      this.replies[userId].actionResponses = [];
+        const actionRequests = this.actionRequestPool[userId];
+        const user = this.users[userId];
+        // debugger;
+        let horizontal_move = user.rotateInfo.horizontal_move;
+        //const traceIds = user.traceIds;
+        let sub = 0;
+        for (let i = 0; i < actionRequests.length; ++i) {
+          if (actionRequests[i].action_type == this.Actions.Rotation) {
+            horizontal_move +=
+              actionRequests[i].rotation_action.horizontal_move;
+            reply.traceIds.push(actionRequests[i].trace_id);
+            const actionResponse = this.createActionResponse(
+              actionRequests[i].action_type,
+              actionRequests[i].trace_id,
+            );
+            reply.actionResponses.push(actionResponse);
+            ++sub;
+          } else {
+            break;
+          }
+        }
 
 
-      return reply;
-      */
-    } catch (error) {
-      console.log('RotateService', error);
-    }
+        actionRequests.splice(0, sub);
+        const hAngle = horizontal_move * 90;
+        if (Math.abs(hAngle) < 1) {
+          user.rotateInfo.horizontal_move = horizontal_move;
+          //user.traceIds = traceIds;
+          this.replies[userId] = reply;
+          return null;
+        }
+        reply = await this.rotateForAngle(userId, Math.floor(hAngle));
+        //return reply;
+        return resolve(reply);
+      } catch (error) {
+        console.log('RotateService', error);
+        return reject(error);
+      }
+    });
   }
   }
 
 
-  async rotateForAngle(userId, hAngle) {
-    try {
-      const user = this.users[userId];
-      user.camera.angle.yaw += Math.floor(hAngle);
-      if (user.camera.angle.yaw < 0) {
-        user.camera.angle.yaw = 360 + user.camera.angle.yaw;
-      } else if (user.camera.angle.yaw > 359) {
-        user.camera.angle.yaw -= 360;
-      }
+  // test(){
+  //   while(true){
+  //     if(this.actionRequests.length>0){
+  //       let actionRequest = this.actionRequests[0];
+  //       //执行
+  //       //添加队列
+  //     }
+  //   }
+  // }
 
 
-      const reply = JSON.parse(JSON.stringify(this.replies[userId]));
-      reply['newUserStates'][0]['userId'] = userId;
-      //从redis里取
-      //let key = user.appId + "-"+user.breakPointId+"-"+user.rotateInfo.frameIndex;
-      const key =
-        'rotateframe:app_id:' +
-        user.appId +
-        ':frame_index:' +
-        user.camera.angle.yaw +
-        ':break_point_id:' +
-        user.breakPointId;
-      // const value = null;
+  // receiveRotate(actionRequest){
+  //   this.actionRequests.push(actionRequest)
+  // }
 
 
-      const redisData = await this.cacheService.get(key);
-      //if (redisData && redisData.length > 0) {
-      const value = JSON.parse(redisData); //redisData ? JSON.parse(redisData) : null;
-      // console.log('rotate-service', value);
-      user.camera['position'] = value.cameraPosition; //value ? value.cameraPosition : '';
-      user.camera['angle'] = value.cameraAngle; //value ? value.cameraAngle : '';
+  rotateForAngle(userId, hAngle): Promise<any> {
+    return new Promise(async (resolve, reject) => {
+      try {
+        const user = this.users[userId];
+        // console.log(
+        //   '当前镜头角度, yaw: %s, hAngle: %s',
+        //   user.camera.angle.yaw,
+        //   hAngle,
+        // );
+        // console.time('当前镜头角度 %s', user.camera.angle.yaw, '旋转角度:', hAngle);
+        user.camera.angle.yaw += Math.floor(hAngle);
+        if (user.camera.angle.yaw < 0) {
+          user.camera.angle.yaw = 360 + user.camera.angle.yaw;
+        } else if (user.camera.angle.yaw > 359) {
+          user.camera.angle.yaw -= 360;
+        }
+        const reply = JSON.parse(JSON.stringify(this.replies[userId]));
+        reply['newUserStates'][0]['userId'] = userId;
+        //从redis里取
+        //let key = user.appId + "-"+user.breakPointId+"-"+user.rotateInfo.frameIndex;
+        const key =
+          'rotateframe:app_id:' +
+          user.appId +
+          ':frame_index:' +
+          user.camera.angle.yaw +
+          ':break_point_id:' +
+          user.breakPointId;
+        // const value = null;
 
 
-      reply['newUserStates'][0]['playerState'].player.position =
-        user.player.position;
-      reply['newUserStates'][0]['playerState'].player.angle = user.player.angle;
+        const redisData = await this.cacheService.get(key);
+        //if (redisData && redisData.length > 0) {
+        const value = JSON.parse(redisData); //redisData ? JSON.parse(redisData) : null;
+        // console.log('rotate-service', value);
+        user.camera['position'] = value.cameraPosition; //value ? value.cameraPosition : '';
+        user.camera['angle'] = value.cameraAngle; //value ? value.cameraAngle : '';
 
 
-      //this.reply['newUserStates'][0]['playerState'] .player
-      reply['newUserStates'][0]['playerState'].camera.position =
-        value.cameraPosition;
-      reply['newUserStates'][0]['playerState'].camera.angle = value.cameraAngle;
-      reply['newUserStates'][0]['playerState'].cameraCenter =
-        user.player.position;
-      // debugger
-      reply.mediaSrc =
-        '/' +
-        user.appId +
-        '/' +
-        user.breakPointId +
-        '/' +
-        value.directory +
-        '/' +
-        value.fileName +
-        '?m=' +
-        new Date().getTime();
-      reply.breakPointId = user.breakPointId;
-      this.replies[userId].traceIds = [];
-      this.replies[userId].actionResponses = [];
+        reply['newUserStates'][0]['playerState'].player.position =
+          user.player.position;
+        reply['newUserStates'][0]['playerState'].player.angle =
+          user.player.angle;
 
 
-      user.rotateInfo.horizontal_move = 0;
+        //this.reply['newUserStates'][0]['playerState'] .player
+        reply['newUserStates'][0]['playerState'].camera.position =
+          value.cameraPosition;
+        reply['newUserStates'][0]['playerState'].camera.angle =
+          value.cameraAngle;
+        reply['newUserStates'][0]['playerState'].cameraCenter =
+          user.player.position;
+        // debugger
+        reply.mediaSrc =
+          '/' +
+          user.appId +
+          '/' +
+          user.breakPointId +
+          '/' +
+          value.directory +
+          '/' +
+          value.fileName +
+          '?m=' +
+          new Date().getTime();
+        reply.breakPointId = user.breakPointId;
+        this.replies[userId].traceIds = [];
+        this.replies[userId].actionResponses = [];
 
 
-      return reply;
-      //}
-      // else {
-      //   return null;
-      // }
-    } catch (error) {
-      this.logger.error('rotateForAngle::function', error);
-    }
+        user.rotateInfo.horizontal_move = 0;
+        // console.timeEnd('旋转后镜头角度:' + user.camera.angle.yaw);
+        // console.log('旋转后镜头角度:, yaw: %s, ', user.camera.angle.yaw);
+        return resolve(reply);
+        //}
+        // else {
+        //   return null;
+        // }
+      } catch (error) {
+        this.logger.error('rotateForAngle::function', error);
+        return reject(error);
+      }
+    });
   }
   }
 
 
   createActionResponse(actionType, traceId) {
   createActionResponse(actionType, traceId) {

+ 109 - 109
src/scene/scene.service.ts

@@ -15,6 +15,7 @@ import { DelayQueue, RxQueue, ThrottleQueue, DebounceQueue } from 'rx-queue';
 import { MoveService } from 'src/move/move.service';
 import { MoveService } from 'src/move/move.service';
 import { GetRouterService } from 'src/get-router/get-router.service';
 import { GetRouterService } from 'src/get-router/get-router.service';
 import { ConfigService } from '@nestjs/config';
 import { ConfigService } from '@nestjs/config';
+import { join } from 'path';
 
 
 @Injectable()
 @Injectable()
 export class SceneService implements OnModuleInit, OnModuleDestroy {
 export class SceneService implements OnModuleInit, OnModuleDestroy {
@@ -58,13 +59,15 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   private clickQueueSub: any;
   private clickQueueSub: any;
 
 
   private streamServiceSub: any;
   private streamServiceSub: any;
-  private roQueue: RxQueue = new DelayQueue(30);
+  private roRequestQueue: RxQueue = new RxQueue(5);
+  private roQueue: RxQueue = new DelayQueue(20);
   private clickQueue: RxQueue = new DebounceQueue(500);
   private clickQueue: RxQueue = new DebounceQueue(500);
   private moveQueue: RxQueue = new DelayQueue(30);
   private moveQueue: RxQueue = new DelayQueue(30);
   private joystickQueue: RxQueue = new DebounceQueue(500);
   private joystickQueue: RxQueue = new DebounceQueue(500);
   private requestIFrameQueue: RxQueue = new DebounceQueue(2000);
   private requestIFrameQueue: RxQueue = new DebounceQueue(2000);
 
 
   private requestIFrameQueueSub: any;
   private requestIFrameQueueSub: any;
+  private roRequestQueueSub: any;
   private rotateTimeStamp: number;
   private rotateTimeStamp: number;
   private lastMoveCnt = -1;
   private lastMoveCnt = -1;
 
 
@@ -216,87 +219,101 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   }
   }
 
 
   async rotate(request: RotateRequest) {
   async rotate(request: RotateRequest) {
-    try {
-      if (this.firstRender) {
-        if (!this.roQueueSubscription) {
-          this.handleRotateStream();
-        }
-        let redisMeta: StreamReplyType;
-        this.onRotating.next(true);
-        if (this.onMoving.value) {
-          this.onMoving.next(false);
-          const lastStreamFrame = this.lastMoveStreamFrame.getValue();
-          const metaData: StreamReplyType = JSON.parse(
-            lastStreamFrame.metaData,
-          ) as any as StreamReplyType;
-          const newUserStates: NewUserStatesType = metaData.newUserStates.find(
-            (item) => item.userId === this.user_id,
-          );
-          const trace_id = metaData.traceIds[0];
-          const userId = newUserStates.userId;
-          const breakPointId = metaData.breakPointId;
-          const cameraAngle = newUserStates.playerState.camera.angle;
-          const playerAngle = newUserStates.playerState.player.angle;
-          console.log('stop-data', trace_id, userId, cameraAngle, cameraAngle);
-          redisMeta = await this.moveService.stop(
-            trace_id,
-            userId,
-            breakPointId,
-            cameraAngle,
-            playerAngle,
-          );
-          console.log('stop-redisMeta', redisMeta);
-          // redisMeta = await this.rotateService.rotate(request);
-        } else {
-          redisMeta = await this.rotateService.rotate(request);
-        }
+    this.roRequestQueue.next(request);
+    if (!this.roRequestQueueSub) {
+      this.handleRotate();
+    }
+  }
+  /**
+   * rotate请求队列
+   */
 
 
-        if (redisMeta && 'mediaSrc' in redisMeta) {
-          const mediaSrc: string = redisMeta.mediaSrc || '';
-          if (mediaSrc.length > 0) {
-            const src = mediaSrc.split('?')[0];
-            // 临时本地替换路经
-            // src = src.replace('/10086/', '');
-            // 判断不是同一条源时才推出
-            if (src.length > 0) {
-              // console.log('不同源');
-              // this.frameCnt += 1;
-              this.holdSteam();
-              this.lastRenderMedia = src;
-              // const clipPath = join(__dirname, `../ws/${src}`);
-              const clipPath = this.configService.get('app.prefix') + src;
-              // console.log('src-clipPath', src, clipPath);
-              delete redisMeta.mediaSrc;
-
-              // const nextFrame = this.frameCnt.getValue() + 1;
-              // console.log('nextFrame', nextFrame);
-              // this.frameCnt.next(nextFrame);
-              const random_boolean = Math.random() < 0.3;
-
-              const stream: StreamFrameType = {
-                frame: -1,
-                clipPath: clipPath,
-                metaData: JSON.stringify(redisMeta),
-                serverTime: this.mockserverTime,
-                DIR: this.frameCnt.getValue() < 10 ? 3 : random_boolean ? 1 : 3,
-              };
-              // this.rotateQueue.add(stream, {
-              //   delay: 5,
-              //   jobId: `rotate:${this.user_id}:${this.frameCnt.getValue()}`,
-              //   removeOnComplete: true,
-              // });
-              clearTimeout(this._rotateTimeout);
-              this.roQueue.next(stream);
+  handleRotate() {
+    this.roRequestQueueSub = this.roRequestQueue.subscribe(
+      async (request: RotateRequest) => {
+        try {
+          if (this.firstRender) {
+            if (!this.roQueueSubscription) {
+              this.handleRotateStream();
+            }
+            let redisMeta: StreamReplyType;
+            this.onRotating.next(true);
+            if (this.onMoving.value) {
+              this.onMoving.next(false);
+              const lastStreamFrame = this.lastMoveStreamFrame.getValue();
+              const metaData: StreamReplyType = JSON.parse(
+                lastStreamFrame.metaData,
+              ) as any as StreamReplyType;
+              const newUserStates: NewUserStatesType =
+                metaData.newUserStates.find(
+                  (item) => item.userId === this.user_id,
+                );
+              const trace_id = metaData.traceIds[0];
+              const userId = newUserStates.userId;
+              const breakPointId = metaData.breakPointId;
+              const cameraAngle = newUserStates.playerState.camera.angle;
+              const playerAngle = newUserStates.playerState.player.angle;
+              console.log(
+                'stop-data',
+                trace_id,
+                userId,
+                cameraAngle,
+                cameraAngle,
+              );
+              redisMeta = await this.moveService.stop(
+                trace_id,
+                userId,
+                breakPointId,
+                cameraAngle,
+                playerAngle,
+              );
+              console.log('stop-redisMeta', redisMeta);
+              // redisMeta = await this.rotateService.rotate(request);
             } else {
             } else {
-              // this.onRotating.next(false);
+              redisMeta = await this.rotateService.rotate(request);
+            }
+
+            if (redisMeta && 'mediaSrc' in redisMeta) {
+              const mediaSrc: string = redisMeta.mediaSrc || '';
+              if (mediaSrc.length > 0) {
+                const src = mediaSrc.split('?')[0];
+
+                if (src.length > 0) {
+                  // console.log('不同源');
+                  this.holdSteam();
+                  this.lastRenderMedia = src;
+                  const clipPath = this.configService.get('app.prefix') + src;
+                  delete redisMeta.mediaSrc;
+
+                  const random_boolean = Math.random() < 0.3;
+
+                  const stream: StreamFrameType = {
+                    frame: -1,
+                    clipPath: clipPath,
+                    metaData: JSON.stringify(redisMeta),
+                    serverTime: this.mockserverTime,
+                    DIR:
+                      this.frameCnt.getValue() < 10
+                        ? 3
+                        : random_boolean
+                          ? 1
+                          : 3,
+                  };
+                  console.log('rotate', stream, Date.now());
+                  clearTimeout(this._rotateTimeout);
+                  this.roQueue.next(stream);
+                } else {
+                  // this.onRotating.next(false);
+                }
+              }
             }
             }
           }
           }
+        } catch (error) {
+          this.logger.error('rotate', error);
+          console.log('error', error);
         }
         }
-      }
-    } catch (error) {
-      this.logger.error('rotate', error);
-      console.log('error', error);
-    }
+      },
+    );
   }
   }
 
 
   async walking(req) {
   async walking(req) {
@@ -315,10 +332,11 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           user.breakPointId,
           user.breakPointId,
           req.clicking_action.clicking_point,
           req.clicking_action.clicking_point,
         );
         );
+        // debugger;
         const walkingRes = await this.moveService.move(path, request);
         const walkingRes = await this.moveService.move(path, request);
 
 
-        console.log('walkingRes-length', Array.from(walkingRes).flat().length);
-
+        console.log('walking', walkingRes);
+        debugger;
         if (walkingRes && !this.onMoving.value) {
         if (walkingRes && !this.onMoving.value) {
           // console.log('walkingRes-front', walkingRes);
           // console.log('walkingRes-front', walkingRes);
           // shift出前第一个镜头数据
           // shift出前第一个镜头数据
@@ -338,18 +356,20 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             walkingRes,
             walkingRes,
           ).flat() as any as StreamReplyType[];
           ).flat() as any as StreamReplyType[];
 
 
-          if (seqs?.length) {
+          if (rotateCamData?.length || seqs?.length) {
             const lastSeq = rotateCamData?.length
             const lastSeq = rotateCamData?.length
               ? (Array.from(rotateCamData).concat(
               ? (Array.from(rotateCamData).concat(
                 seqs,
                 seqs,
               ) as any as StreamReplyType[])
               ) as any as StreamReplyType[])
               : seqs;
               : seqs;
-            console.log('walking --总序列--seqs', seqs.length);
+
+            console.log('walking --总序列--seqs-2', seqs.length);
             this.handleSeqMoving(lastSeq);
             this.handleSeqMoving(lastSeq);
           } else {
           } else {
             console.error('walking-move无数据');
             console.error('walking-move无数据');
             this.cleanMoveSteam();
             this.cleanMoveSteam();
           }
           }
+          debugger;
           // this.lastMoveCnt = this.frameCnt.value + seqs.length;
           // this.lastMoveCnt = this.frameCnt.value + seqs.length;
         }
         }
       });
       });
@@ -527,9 +547,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   handleDataChanelOpen(channel: DataChannel): void {
   handleDataChanelOpen(channel: DataChannel): void {
     this.channel = channel;
     this.channel = channel;
     this.streamService.setChannel(channel);
     this.streamService.setChannel(channel);
-    // this.startSteaming.next(true);
-    // this.startStream();
-    // this.handleStream();
   }
   }
 
 
   handleDataChanelClose(): void {
   handleDataChanelClose(): void {
@@ -593,14 +610,12 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
 
   async handleIframeRequest() {
   async handleIframeRequest() {
     //TODO Iframe 最终传什么?
     //TODO Iframe 最终传什么?
-
     this.requestIFrameQueue.next(this.streamService.lastStreamFrame.getValue());
     this.requestIFrameQueue.next(this.streamService.lastStreamFrame.getValue());
-
     if (!this.requestIFrameQueueSub) {
     if (!this.requestIFrameQueueSub) {
       this.requestIFrameQueueSub = this.requestIFrameQueue.subscribe(
       this.requestIFrameQueueSub = this.requestIFrameQueue.subscribe(
         (frameData: StreamFrameType) => {
         (frameData: StreamFrameType) => {
           const nextFrame = this.frameCnt.getValue() + 1;
           const nextFrame = this.frameCnt.getValue() + 1;
-          this.logger.warn('lostIframe', nextFrame,);
+          this.logger.warn('lostIframe', nextFrame);
           frameData.frame = nextFrame;
           frameData.frame = nextFrame;
           this.streamService.pushFrameToSteam(frameData);
           this.streamService.pushFrameToSteam(frameData);
           this.frameCnt.next(nextFrame);
           this.frameCnt.next(nextFrame);
@@ -608,28 +623,11 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
         },
         },
       );
       );
     }
     }
-
-    // const lastStreamFrame = this.streamService.lastStreamFrame.getValue();
-    // lastStreamFrame.DIR = 1;
-    // console.log('lastStreamFrame', lastStreamFrame);
-    // const nextFrame = this.frameCnt.getValue() + 1;
-    // lastStreamFrame.frame = nextFrame;
-    // this.frameCnt.next(nextFrame);
-    // this.streamService.pushFrameToSteam(lastStreamFrame);
-    // const redisDataAuto = await this.rotateService.echo(this.user_id);
-    // if (redisDataAuto) {
-    //   'mediaSrc' in redisDataAuto && delete redisDataAuto.mediaSrc;
-    //   const streamMeta: StreamMetaType = {
-    //     frame: nextFrame,
-    //     metaData: JSON.stringify(redisDataAuto),
-    //   };
-    //   this.streamService.pushMetaDataToSteam(streamMeta);
-    // }
   }
   }
 
 
-  async handleBreath(request) {
-    const npsRes = await this.moveService.getBreakPoints(request);
-    // console.log('npsRes', npsRes);
+  handleBreath(request) {
+    const npsRes = this.moveService.getBreakPoints(request);
+    console.log('npsRes', npsRes);
     this.streamService.pushNormalDataToStream(npsRes);
     this.streamService.pushNormalDataToStream(npsRes);
   }
   }
 
 
@@ -745,6 +743,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       });
       });
     }
     }
   }
   }
+  /**
+   * rotate 推送队列
+   */
   handleRotateStream() {
   handleRotateStream() {
     if (!this.roQueueSubscription) {
     if (!this.roQueueSubscription) {
       this.roQueueSubscription = this.roQueue.subscribe(
       this.roQueueSubscription = this.roQueue.subscribe(
@@ -771,8 +772,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           if (res.done) {
           if (res.done) {
             clearTimeout(this._rotateTimeout);
             clearTimeout(this._rotateTimeout);
             this._rotateTimeout = setTimeout(() => {
             this._rotateTimeout = setTimeout(() => {
-              console.log('rotate 1秒', Date.now());
-              console.log('rotate end');
+              console.log('rotate end', Date.now());
               // const next = res.frame + 1;
               // const next = res.frame + 1;
               this.frameCnt.next(res.frame);
               this.frameCnt.next(res.frame);
               this.resumeStream();
               this.resumeStream();

+ 8 - 3
src/scene/stream/stream.service.ts

@@ -5,6 +5,7 @@ import { readFileSync } from 'fs';
 import * as streamBuffers from 'stream-buffers';
 import * as streamBuffers from 'stream-buffers';
 import { BehaviorSubject } from 'rxjs';
 import { BehaviorSubject } from 'rxjs';
 import { CacheService } from 'src/cache/cache.service';
 import { CacheService } from 'src/cache/cache.service';
+import { join } from 'path';
 
 
 @Injectable()
 @Injectable()
 export class StreamService {
 export class StreamService {
@@ -93,15 +94,19 @@ export class StreamService {
   pushFrameToSteam(stream: StreamFrameType): Promise<StreamPushResponse> {
   pushFrameToSteam(stream: StreamFrameType): Promise<StreamPushResponse> {
     return new Promise((resolve, reject) => {
     return new Promise((resolve, reject) => {
       try {
       try {
-        // if (!this.onSteaming) {
-        const clipPath = stream.clipPath;
+        // console.log('process.env', process.env.node_env);
+        const src = stream.clipPath.replace('/mnt/metaverse/scene', '');
+        const srcTmp = join(__dirname, `../ws/${src}`);
+        const clipPath = srcTmp;
+
+        // const clipPath = stream.clipPath;
         const metaData = stream.metaData || '{}';
         const metaData = stream.metaData || '{}';
         const frame = stream.frame;
         const frame = stream.frame;
         const serverTime = stream.serverTime || 754871824;
         const serverTime = stream.serverTime || 754871824;
         const dir = stream.DIR || 1;
         const dir = stream.DIR || 1;
 
 
         this.lastStreamFrame.next({
         this.lastStreamFrame.next({
-          clipPath: clipPath,
+          clipPath: stream.clipPath,
           metaData: metaData,
           metaData: metaData,
           frame: frame,
           frame: frame,
           serverTime: serverTime,
           serverTime: serverTime,

+ 1 - 0
tsconfig.json

@@ -17,6 +17,7 @@
     "strictBindCallApply": false,
     "strictBindCallApply": false,
     "forceConsistentCasingInFileNames": false,
     "forceConsistentCasingInFileNames": false,
     "noFallthroughCasesInSwitch": false,
     "noFallthroughCasesInSwitch": false,
+    "resolveJsonModule": true,
     "paths": {
     "paths": {
       "@app/utils": [
       "@app/utils": [
         "libs/utils/src"
         "libs/utils/src"

+ 0 - 27
ws/HeartBeat.js

@@ -1,27 +0,0 @@
-export default class Heartbeat {
-  constructor(e) {
-    E(this, '_interval', null);
-    E(this, 'ping', () => {
-      const e = Date.now().toString();
-      this.handler.ping(e);
-    });
-    this.handler = e;
-  }
-  ping() {
-    const e = Date.now().toString();
-    this.handler.ping(e);
-  }
-  start() {
-    this.stop(),
-      logger.debug(`Setting ping interval to ${PING_INTERVAL_MS}ms`),
-      (this._interval = window.setInterval(this.ping, PING_INTERVAL_MS));
-  }
-  stop() {
-    logger.debug('stop heartbeat'),
-      this._interval && window.clearInterval(this._interval);
-  }
-  pong(e, t) {
-    !e ||
-      (typeof e == 'string' && this.handler.pong(Date.now() - Number(e), t));
-  }
-}

+ 0 - 24
ws/index.html

@@ -1,24 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
-    <title>test socket</title>
-  </head>
-  <body>
-    <script type="module">
-      const socket = new WebSocket('ws://localhost:3100/ws?id=1111');
-      socket.addEventListener('message', (event) => {
-        console.log('event', event);
-      });
-      const test_message = {
-        data: JSON.stringify({ is_mobile: true }),
-        id: 'init_webrtc',
-      };
-      socket.onopen = (event) => {
-        console.log('onopen', event);
-        socket.send(JSON.stringify(test_message));
-      };
-    </script>
-  </body>
-</html>