gemercheung 3 lat temu
rodzic
commit
3bc46aae06

+ 59 - 40
src/meta.gateway.ts

@@ -23,6 +23,7 @@ import * as path from 'path';
 import { createReadStream } from 'fs';
 import { SceneService } from './scene/scene.service';
 import { ConfigService } from '@nestjs/config';
+
 // 'Verbose' | 'Debug' | 'Info' | 'Warning' | 'Error' | 'Fatal';
 initLogger('Debug');
 
@@ -45,6 +46,9 @@ export class MetaGateway
   private _webrtcInterval: NodeJS.Timeout;
   private heartBeatFlag: number;
   private gameChanel: DataChannel;
+  private user_id: string;
+  private roomId: string;
+
   @WebSocketServer() server: Server;
 
   // @SubscribeMessage('message')
@@ -57,7 +61,7 @@ export class MetaGateway
 
   @SubscribeMessage('init')
   handleInit(client: any, payload: any) {
-    this.logger.log(`init: ${JSON.stringify(payload)}`);
+    this.logger.log(`socket::init: ${JSON.stringify(payload)}`);
   }
 
   @SubscribeMessage('heartbeat')
@@ -180,7 +184,6 @@ export class MetaGateway
 
     this.gameChanel.onOpen(() => {
       console.log('channel is open');
-
       this.sceneService.handleDataChanelOpen(this.gameChanel);
 
       clearInterval(this.timer);
@@ -269,51 +272,67 @@ 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,
-    );
-
-    const demoVal = {
-      id: 'start',
-      data: '{"IsHost":false,"SkinID":"10089","SkinDataVersion":"1008900008","RoomTypeID":""}',
-      room_id: 'e629ef3e-022d-4e64-8654-703bb96410eb',
-      channel_id: '4f2cf027d926f3cc___channel',
-      user_id: '15bc22119cfa1',
-      trace_id: '45a925d6-7d05-4c11-93a3-0fdaabd7db6f',
-      packet_id: '',
-      session_id: '4bbfd45cbeab43d3a9b992349f00dfba',
-      client_os: '',
-      fe_version: '',
-      is_browser: false,
-    };
+    try {
+      const obj = JSON.parse(payload);
+      const requestPayLoad: InitRequest = Object.assign({}, obj, {
+        user_id: this.user_id,
+        roomId: this.roomId,
+      });
+      this.sceneService.init(requestPayLoad);
+      this.logger.log(
+        'start and send to gprc sceneService,method=>init',
+        requestPayLoad,
+      );
 
-    const startReply = {
-      id: 'start',
-      data: '{"IsHost":false,"SkinID":"10089","SkinDataVersion":"1008900008","RoomTypeID":""}',
-      room_id: 'e629ef3e-022d-4e64-8654-703bb96410eb',
-      channel_id: '3a1a62e9a3c74de6___channel',
-      user_id: 'ed58c8d4ce38c',
-      trace_id: '394df10a-d924-43a9-940d-1dbb41e43f24',
-      packet_id: '',
-      session_id: '67087ad820ea4c89af311e27281d73a6',
-      client_os: '',
-      fe_version: '',
-    };
-    this.sceneService.onSteaming.subscribe((val) => {
-      if (val) {
-        console.log('onSteaming-start', val);
-        client.send(JSON.stringify(startReply));
-      }
-      // debugger
-    });
+      // const demoVal = {
+      //   id: 'start',
+      //   data: '{"IsHost":false,"SkinID":"10089","SkinDataVersion":"1008900008","RoomTypeID":""}',
+      //   room_id: 'e629ef3e-022d-4e64-8654-703bb96410eb',
+      //   channel_id: '4f2cf027d926f3cc___channel',
+      //   user_id: '15bc22119cfa1',
+      //   trace_id: '45a925d6-7d05-4c11-93a3-0fdaabd7db6f',
+      //   packet_id: '',
+      //   session_id: '4bbfd45cbeab43d3a9b992349f00dfba',
+      //   client_os: '',
+      //   fe_version: '',
+      //   is_browser: false,
+      // };
+
+      const startReply = {
+        id: 'start',
+        data: '{"IsHost":false,"SkinID":"10089","SkinDataVersion":"1008900008","RoomTypeID":""}',
+        room_id: 'e629ef3e-022d-4e64-8654-703bb96410eb',
+        channel_id: '3a1a62e9a3c74de6___channel',
+        user_id: 'ed58c8d4ce38c',
+        trace_id: '394df10a-d924-43a9-940d-1dbb41e43f24',
+        packet_id: '',
+        session_id: '67087ad820ea4c89af311e27281d73a6',
+        client_os: '',
+        fe_version: '',
+      };
+      this.sceneService.onSteaming.subscribe((val) => {
+        if (val) {
+          console.log('onSteaming-start', val);
+          client.send(JSON.stringify(startReply));
+        }
+        // debugger
+      });
+    } catch (error) {}
   }
 
   handleConnection(client: WebSocket, ...args: any[]) {
     const { url } = args[0];
     console.log('url', url);
+    const params = new URLSearchParams(url.replace('/ws?', ''));
+
+    console.log('useId', params.get('userId'));
+    console.log('roomId', params.get('roomId'));
+
+    this.user_id = params.get('userId');
+    this.roomId = params.get('roomId');
+
     this.logger.log(`Client connected:`);
+
     const connected = {
       channel_id: '',
       client_os: '',

+ 1 - 1
src/scene/grpc-scene.options.ts

@@ -1,7 +1,7 @@
 import { Transport, ClientOptions } from '@nestjs/microservices';
 import { join } from 'path';
 import configuration from 'src/config/configuration';
-
+console.log('grpc.url', configuration().grpc.url);
 export const grpcClientOptions: ClientOptions = {
   transport: Transport.GRPC,
   options: {

+ 108 - 16
src/scene/scene.service.ts

@@ -5,7 +5,7 @@ import { Logger } from '@nestjs/common';
 import { DataChannel } from 'node-datachannel';
 import * as path from 'path';
 import { statSync, createReadStream, readFileSync } from 'fs';
-import { Readable } from 'stream';
+// import { Readable } from 'stream';
 import { BehaviorSubject } from 'rxjs';
 import * as streamBuffers from 'stream-buffers';
 import { ActionType } from './actionType';
@@ -23,6 +23,8 @@ export class SceneService implements OnModuleInit {
   public _frameInteval: NodeJS.Timeout;
   private channel: DataChannel;
   public onSteaming = new BehaviorSubject<boolean>(false);
+  public user_id: string;
+  public roomId: string;
 
   onModuleInit() {
     this.sceneGrpcService =
@@ -41,17 +43,10 @@ export class SceneService implements OnModuleInit {
     return this.sceneGrpcService.test(request);
   }
 
-  init(request: string) {
+  init(request: InitRequest) {
     try {
-      // console.log('request', this.sceneGrpcService);
-      const params = JSON.parse(request);
-      // const requestData: InitRequest = {
-      //   appId: params.appId
-      // }
-
-      this.logger.log('init', request);
-      const rpcRequest: InitRequest = params;
-      const initReply = this.sceneGrpcService.init(rpcRequest);
+      // this.logger.log('rtc::init::start', JSON.stringify(request));
+      const initReply = this.sceneGrpcService.init(request);
       initReply.subscribe((reply) => {
         console.log('initReply', reply);
       });
@@ -66,6 +61,7 @@ export class SceneService implements OnModuleInit {
 
   rotate(request: RotateRequest) {
     const reply = this.sceneGrpcService.rotate(request);
+    console.log('reply', reply);
     reply.subscribe((res) => {
       console.log('rotate-reply', res);
     });
@@ -90,13 +86,19 @@ export class SceneService implements OnModuleInit {
     try {
       if (typeof message === 'string') {
         const msg: RTCMessageRequest = JSON.parse(message);
+        console.log('msg', msg);
         switch (msg.action_type) {
           case ActionType.rotate:
             console.log('rotate', msg);
             const rotateRequest: RotateRequest = msg;
             this.rotate(rotateRequest);
             break;
-
+          case ActionType.userStatus:
+            this.updateUserStatus();
+            break;
+          case ActionType.status:
+            this.updateStatus();
+            break;
           default:
             break;
         }
@@ -106,15 +108,105 @@ export class SceneService implements OnModuleInit {
       this.logger.error('handleMessage:rtc', error);
     }
   }
+  updateStatus() {
+    const reply = {
+      data: { action_type: 1009, echo_msg: { echoMsg: '1652857098550' } },
+      track: false,
+    };
+    if (this.channel && this.channel.isOpen()) {
+      const replyBin = JSON.stringify(reply).replace(/\s/g, '');
+      const buff = Buffer.from(replyBin, 'utf-8');
+      this.channel.sendMessageBinary(buff);
+    }
+  }
+
+  updateUserStatus() {
+    const reply = {
+      actionType: 1024,
+      pointType: 100,
+      extra: '',
+      traceId: '65db8e1b-2261-42eb-8bc5-8dc97bfe0b0e',
+      packetId: '',
+      nps: [],
+      peopleNum: 0,
+      zoneId: '',
+      echoMsg: '',
+      reserveDetail: null,
+      userWithAvatarList: [],
+      newUserStates: [
+        {
+          userId: 'e497b92704f5a',
+          playerState: {
+            roomTypeId: '',
+            person: 0,
+            avatarId: 'KGe_Boy',
+            skinId: '10089',
+            roomId: 'e629ef3e-022d-4e64-8654-703bb96410eb',
+            isHost: false,
+            isFollowHost: false,
+            skinDataVersion: '1008900008',
+            avatarComponents: '',
+            nickName: 'e497b92704f5a',
+            movingMode: 0,
+            attitude: 'walk',
+            areaName: '',
+            pathName: 'thirdwalk',
+            pathId: 'thirdwalk',
+            avatarSize: 1,
+            extra: '{"removeWhenDisconnected":true}',
+            prioritySync: false,
+            avatarURL: '',
+            micStatus: 0,
+            player: {
+              position: { x: -755, y: -1450, z: -34 },
+              angle: { pitch: 0, yaw: 0, roll: 0 },
+            },
+            camera: null,
+            cameraCenter: null,
+          },
+          renderInfo: {
+            renderType: 0,
+            videoFrame: null,
+            cameraStateType: 0,
+            isMoving: 0,
+            needIfr: 0,
+            isVideo: 0,
+            stillFrame: 0,
+            isRotating: 0,
+            isFollowing: 0,
+            clientPanoTitlesBitmap: [],
+            clientPanoTreceId: '',
+            prefetchVideoId: '',
+            noMedia: false,
+          },
+          event: {
+            id: '',
+            type: 0,
+            points: [],
+            rotateEvent: null,
+            removeVisitorEvent: null,
+          },
+          relation: 0,
+        },
+      ],
+      code: 0,
+      msg: '',
+    };
+    if (this.channel && this.channel.isOpen()) {
+      const replyBin = JSON.stringify(reply).replace(/\s/g, '');
+      const buff = Buffer.from(replyBin, 'utf-8');
+      this.channel.sendMessageBinary(buff);
+    }
+  }
   handleStartCountingFrame() {
     this._frameInteval = setInterval(async () => {
       this.frameCnt += 1;
       try {
         // console.log()
-        const data = await this.cacheService.get(
-          'updateFrameMetadata:2f60ea15cbd5a2',
-        );
-        console.log('test-data', data);
+        // const data = await this.cacheService.get(
+        //   'updateFrameMetadata:2f60ea15cbd5a2',
+        // );
+        // console.log('test-data', data);
 
         if (this.frameCnt === 1) {
           this.pushTheFirstFrame();

+ 5 - 0
src/scene/stream/stream.d.ts

@@ -0,0 +1,5 @@
+interface StreamFrameType {
+  frame: number;
+  clipPath: string;
+  mediaLen: string;
+}

+ 10 - 1
src/scene/stream/stream.service.ts

@@ -1,4 +1,13 @@
 import { Injectable } from '@nestjs/common';
+import { DataChannel } from 'node-datachannel';
 
 @Injectable()
-export class StreamService {}
+export class StreamService {
+  private channel: DataChannel;
+
+  setChannel(channel: DataChannel) {
+    this.channel = channel;
+  }
+
+  pushFrameToSteam(stream: StreamFrameType) {}
+}