瀏覽代碼

add redis module

gemercheung 3 年之前
父節點
當前提交
5c5e164181
共有 11 個文件被更改,包括 4954 次插入4851 次删除
  1. 1 0
      package.json
  2. 132 73
      proto/scene.proto
  3. 1 1
      src/app.controller.ts
  4. 2 1
      src/app.module.ts
  5. 13 4
      src/meta.gateway.ts
  6. 21 0
      src/redis/redis.module.ts
  7. 12 0
      src/redis/redis.options.ts
  8. 18 0
      src/redis/redis.service.spec.ts
  9. 24 0
      src/redis/redis.service.ts
  10. 4 0
      src/scene/scene.service.ts
  11. 4726 4772
      yarn.lock

+ 1 - 0
package.json

@@ -33,6 +33,7 @@
     "@nestjs/websockets": "^8.4.4",
     "buffer": "^6.0.3",
     "node-datachannel": "^0.3.2",
+    "redis": "^3",
     "reflect-metadata": "^0.1.13",
     "rimraf": "^3.0.2",
     "rxjs": "^7.2.0"

+ 132 - 73
proto/scene.proto

@@ -7,17 +7,19 @@ option java_package = "com.fdkk.fdkkmeta.grpc";
 
 // grpc的方法
 service SceneGrpcService {
-  // rpc testMethod (SceneRequest) returns (SceneReply){}
   rpc getRoute (RouteRequest) returns (RouteReply){}
   rpc init (InitRequest) returns (NormalReply){}
   rpc rotate (RotateRequest) returns (NormalReply){}
-  rpc move (MoveRequest) returns (MoveReply){}
+//  rpc moveStart (StartMoveRequest) returns (MoveReply){}
+//  rpc moveProcess (MoveNextNeighborRequest) returns (MoveReply){}
+  rpc echo (EchoRequest) returns (EchoReply){}
+  rpc usersState (GetNewUserStateRequest) returns (GetNewUserStateReply){}
   rpc getBreakPoint (BreakPointRequest) returns (BreakPointReply){}
   rpc joystick (JoystickRequest) returns (NormalReply){} //操作杆
 }
 // 全局对象
 message Point {
-  string x=1;     //应该换成double
+  string x=1;
   string y=2;
   string z=3;
 }
@@ -47,15 +49,23 @@ message RouteReply{
   repeated RouteArray in=1;
 }
 
-// // 入参对象
-// message SceneRequest {
-//   string id=1;
-//   string name=2;
-// }
-// // 出参对象
-// message SceneReply {
-//   string res=1;
-// }
+// 入参对象
+message SceneRequest {
+  string id=1;
+  string name=2;
+}
+// 出参对象
+message SceneReply {
+  string res=1;
+}
+
+/**********************************************************/
+// 正常出参对象
+message NormalReply {
+  string code=1;                                       //0/1   0表示没收到,1表示收到
+}
+
+/**********************************************************/
 
 /***********************************************************/
 message Space {
@@ -101,7 +111,7 @@ message RenderInfo{
    string clientPanoTreceId=11;
    string prefetchVideoId=12;
    bool noMedia=13;
-} 
+}
 
 message Event{
    string id=1;
@@ -129,34 +139,90 @@ message InitRequest {
   string room_id=5;
   string app_id=6;
 }
-
 /**********************************************************/
+message echoMsg{
+  string echoMsg=1;
+}
+
+message EchoRequest{
+  int32 action_type=1;   //1009
+  echoMsg echo_msg=2;
+  string trace_id=3;
+  string user_id=4;
+}
+
+message EchoReply{
+  int32 actionType=1;    //1009
+  int32 pointType=2;
+  string extra=3;
+  string traceId=4;
+  string packetId=5;
+  repeated Point nps=6;
+  int32 peopleNum=7;
+  string zoneId=8;
+  string echoMsg=9;
+  string reserveDetail=10;
+  repeated string userWithAvatarList=11;
+  repeated UserState newUserStates=12;
+  int32 code=13;
+  string msg=14;
+}
+
+/*********************************************/
+
+message getNewUserStateAction{
+  int32 userType=1;
+}
+
+message GetNewUserStateRequest{
+  int32 action_type=1;                                           //1024
+  getNewUserStateAction getNewUserStateAction=2;
+  string trace_id=3;
+  string user_id=4;
+}
+
+message GetNewUserStateReply{
+  int32 actionType=1;                                            //1024
+  int32 pointType=2;
+  string extra=3;
+  string traceId=4;
+  string packetId=5;
+  repeated Point nps=6;
+  int32 peopleNum=7;
+  string zoneId=8;
+  string echoMsg=9;
+  string reserveDetail=10;
+  repeated string userWithAvatarList=11;
+  repeated UserState newUserStates=12;
+  int32 code=13;
+  string msg=14;
+}
+/*********************************************/
 //全局对象,旋转的参数
-message rotation_action{
+message RotationAction{
   int32 vertical_move=1;
   double horizontal_move=2;
 }
 
 // rotate入参对象
 message RotateRequest {
-  int32 action_type=1;
-  string rotation_action=2;
+  int32 action_type=1;                                                     //1014
+  RotationAction rotation_action=2;
   string trace_id=3;
   string user_id=4;
 }
-
 /**********************************************************/
 //正常的反馈
-message NormalReply {
-   repeated string traceIds=1;
-   string vehicle=2;
-   repeated UserState newUserStates=3;
-   repeated int32 actionResponses=4;
-   int32 getStateType=5;
-   int32 code=6;
-   string msg=7;
-   string frameSrc=8;              //视频路径
-}
+//message NormalReply {
+//   repeated string traceIds=1;
+//   string vehicle=2;
+//   repeated UserState newUserStates=3;
+//   repeated int32 actionResponses=4;
+//   int32 getStateType=5;
+//   int32 code=6;
+//   string msg=7;
+//   string frameSrc=8;              //视频路径
+//}
 
 /**********************************************************/
 //
@@ -167,52 +233,45 @@ message ClickingAction {
    string attitude=4;
 }
 message MoveRequest {
-  int32 action_type=1;
-  ClickingAction clicking_action=2;
-  State clicking_state=3;
-  string trace_id=4;
-  string user_id=5;
-}
-
-// 开始请求漫游
-message StartMoveRequest {
-  int32 action_type=1;
-  ClickingAction clicking_action=2;
-  State clicking_state=3;
-  string trace_id=4;
-  string user_id=5;
-}
-
-// 请求移动下一段
-message MoveNextNeighborRequest {
-  int32 user_id=1;
-  string trace_id=2;
-  bool next=3;
-}
-
-// 结束移动下一段(一般是有新的请求)
-message EndMoveNextNeighborRequest {
-  int32 user_id=1;
-  string trace_id=2;
-  bool next=3;
+    int32 action_type=1;                                                //1
+    ClickingAction clicking_action=2;
+    State clicking_state=3;
+    string trace_id=4;
+    string user_id=5;
 }
+//// 开始请求漫游
+//message StartMoveRequest {
+//  int32 action_type=1;
+//  ClickingAction clicking_action=2;
+//  State clicking_state=3;
+//  string trace_id=4;
+//  string user_id=5;
+//}
+//
+//// 请求移动下一段
+//message MoveNextNeighborRequest {
+//  string user_id=1;
+//  string trace_id=2;
+//  bool next=3;      //true表示继续拿下一个,false表示结束(一般是有新的请求)
+//}
 
 // move出参对象
 // 这需要自定义
-message MoveReply {
-   repeated string traceIds=1;
-   string vehicle=2;
-   repeated UserState newUserStates=3;      //自己的话,不要有player,camera和cameraCenter。自己对应的这三个属性在8,9,10上,到webrtc服务器上组装
-   repeated int32 actionResponses=4;
-   int32 getStateType=5;
-   int32 code=6;
-   string msg=7;
-   //每一段的人物和相机坐标,第一段可能还有初始旋转
-   repeated Space player=8;
-   repeated Space camera=9;
-   repeated Point cameraCenter=10;
-   bool isEnd=11;     //false表示可以继续移动,true表示这次移动已经结束
-}
+//message MoveReply {
+//   repeated string traceIds=1;
+//   string vehicle=2;
+//   repeated UserState newUserStates=3;      //自己的话,不要有player,camera和cameraCenter。自己对应的这三个属性在8,9,10上,到webrtc服务器上组装
+//   repeated int32 actionResponses=4;
+//   int32 getStateType=5;
+//   int32 code=6;
+//   string msg=7;
+//   //每一段的人物和相机坐标,第一段可能还有初始旋转
+//   repeated Space player=8;
+//   repeated Space camera=9;
+//   repeated Point cameraCenter=10;
+//   repeated string frameSrc=11;
+//   bool isEnd=12;     //false表示可以继续移动,true表示这次移动已经结束
+//}
 /**********************************************************/
 
 /**********************************************************/
@@ -241,14 +300,14 @@ message NeighborPointsAction{
 
 // move入参对象
 message BreakPointRequest{
-  int32 action_type=1;
+  int32 action_type=1;                                              //1004
   NeighborPointsAction get_neighbor_points_action=2;
   string trace_id=3;
   string user_id=4;
 }
 // move出参对象
 message BreakPointReply {
-  int32 actionType=1;
+  int32 actionType=1;                                               //1004
   int32 pointType=2;
   string extra=3;
   string traceId=4;
@@ -263,4 +322,4 @@ message BreakPointReply {
   int32 code=13;
   string msg=14;
 }
-/**********************************************************/
+/**********************************************************/

+ 1 - 1
src/app.controller.ts

@@ -15,7 +15,7 @@ export class AppController implements OnModuleInit {
   // @Client(grpcClientOptions) private readonly client: ClientGrpc;
   // private sceneGrpcService: SceneGrpcService;
 
-  constructor(private readonly appService: AppService) { }
+  constructor(private readonly appService: AppService) {}
 
   onModuleInit() {
     // console.log('this.client', this.client);

+ 2 - 1
src/app.module.ts

@@ -5,6 +5,7 @@ import { AppService } from './app.service';
 import { MetaGateway } from './meta.gateway';
 import { RoomModule } from './room/room.module';
 import { SceneModule } from './scene/scene.module';
+import { RedisModule } from './redis/redis.module';
 
 // const protodir = join(__dirname, '..', 'proto/scene.proto');
 
@@ -23,7 +24,7 @@ import { SceneModule } from './scene/scene.module';
 // ]);
 
 @Module({
-  imports: [ConfigModule.forRoot(), RoomModule, SceneModule],
+  imports: [ConfigModule.forRoot(), RoomModule, SceneModule, RedisModule],
   controllers: [AppController],
   providers: [AppService, MetaGateway],
 })

+ 13 - 4
src/meta.gateway.ts

@@ -32,8 +32,9 @@ initLogger('Debug');
   path: '/ws',
 })
 export class MetaGateway
-  implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
-  constructor(private readonly sceneService: SceneService) { }
+  implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
+{
+  constructor(private readonly sceneService: SceneService) {}
   private logger: Logger = new Logger('MetaGateway');
   private peer: PeerConnection = null;
   private timer: NodeJS.Timeout;
@@ -174,7 +175,7 @@ export class MetaGateway
       clearInterval(this.timer);
 
       const peers = this.peer.getSelectedCandidatePair();
-      console.log('配对成功', peers);
+      this.logger.log('配对成功', peers);
 
       let i = 1;
       const paths = path.join(__dirname, '../ws/video/100');
@@ -215,6 +216,7 @@ export class MetaGateway
     });
     this.gameChanel.onMessage((event) => {
       console.log('gameChanel onMessage', event);
+      this.sceneService.handleMessage(event);
     });
     this.gameChanel.onError(() => {
       console.log('gameChanel onError');
@@ -256,10 +258,17 @@ export class MetaGateway
   @SubscribeMessage('start')
   handlerWebrtcStart(client: any, payload: any) {
     console.log('start', payload);
+    this.sceneService.init(payload);
+    this.logger.log(
+      'start and send to gprc sceneService,method=>init',
+      payload,
+    );
   }
 
   handleConnection(client: WebSocket, ...args: any[]) {
-    this.logger.log(`Client connected: ${args}`);
+    const { url } = args[0];
+    console.log('url', url);
+    this.logger.log(`Client connected:`);
     const connected = {
       channel_id: '',
       client_os: '',

+ 21 - 0
src/redis/redis.module.ts

@@ -0,0 +1,21 @@
+import { Module } from '@nestjs/common';
+// import { ClientsModule, Transport } from '@nestjs/microservices';
+import { RedisService } from './redis.service';
+
+@Module({
+  imports: [
+    // ClientsModule.register([
+    //   {
+    //     name: 'REDIS_SERVICE',
+    //     transport: Transport.REDIS,
+    //     // rediss://<username>:<password>@<host>:<port>
+    //     options: {
+    //       url: 'redis://:1234@192.168.0.152:6379#4',
+    //     },
+    //   },
+    // ]),
+  ],
+  providers: [RedisService],
+  exports: [RedisService],
+})
+export class RedisModule {}

+ 12 - 0
src/redis/redis.options.ts

@@ -0,0 +1,12 @@
+import { Transport, ClientOptions } from '@nestjs/microservices';
+
+export const redisClientOptions: ClientOptions = {
+  transport: Transport.REDIS,
+  options: {
+    // url: process.env.GRPC_URL,
+    url: 'redis://:redis9394@127.0.0.1:6379/',
+    // url: 'redis://:1234@192.168.0.152:6379/4',
+    retryAttempts: 20,
+    retryDelay: 2,
+  },
+};

+ 18 - 0
src/redis/redis.service.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { RedisService } from './redis.service';
+
+describe('RedisService', () => {
+  let service: RedisService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [RedisService],
+    }).compile();
+
+    service = module.get<RedisService>(RedisService);
+  });
+
+  it('should be defined', () => {
+    expect(service).toBeDefined();
+  });
+});

+ 24 - 0
src/redis/redis.service.ts

@@ -0,0 +1,24 @@
+import { Injectable, OnModuleInit } from '@nestjs/common';
+import { ClientRedis, Client } from '@nestjs/microservices';
+import { redisClientOptions } from './redis.options';
+@Injectable()
+export class RedisService implements OnModuleInit {
+  @Client(redisClientOptions) private readonly client: ClientRedis;
+  async onModuleInit() {
+    console.log('redis init');
+    // console.log('this.client', this.client);
+    try {
+      // const opt = this.client.getClientOptions();
+      //   const connect = await this.client.connect();
+      //   const redis = this.client.createClient();
+      //   console.log('connect', connect);
+      // this.client
+    } catch (error) { 
+
+
+      
+    }
+
+    // this.client.createClient();
+  }
+}

+ 4 - 0
src/scene/scene.service.ts

@@ -37,4 +37,8 @@ export class SceneService implements OnModuleInit {
   joystick(request: JoystickRequest) {
     return this.sceneGrpcService.joystick(request);
   }
+
+  handleMessage(event: string | Buffer) {
+    console.log('get rtc message', event);
+  }
 }

文件差異過大導致無法顯示
+ 4726 - 4772
yarn.lock