Browse Source

pravite Ip

gemercheung 3 years ago
parent
commit
ea1f926755
1 changed files with 44 additions and 18 deletions
  1. 44 18
      src/meta.gateway.ts

+ 44 - 18
src/meta.gateway.ts

@@ -10,12 +10,18 @@ import {
 import { Server } from 'ws';
 import * as WebSocket from 'ws';
 
-import { PeerConnection, initLogger, DataChannel } from 'node-datachannel';
+import {
+  PeerConnection,
+  initLogger,
+  DataChannel,
+  cleanup,
+} from 'node-datachannel';
+
 import { Buffer } from 'buffer';
 import { Logger } from '@nestjs/common';
 import * as path from 'path';
 import { createReadStream } from 'fs';
-
+// 'Verbose' | 'Debug' | 'Info' | 'Warning' | 'Error' | 'Fatal';
 initLogger('Debug');
 
 @WebSocketGateway({
@@ -62,14 +68,14 @@ export class MetaGateway
       user_id: '',
     };
 
-    // if (this.gameChanel.isOpen()) {
-    //   // console.log('this.gameChanel.isOpen()', this.gameChanel.isOpen());
-    //   // this.sendWertcHeartPack(this.gameChanel);
-    //   // const heartPack = new DataView(new ArrayBuffer(4));
-    //   // heartPack.setUint32(0, 2009889916);
-    //   // this.gameChanel.sendMessageBinary(Buffer.from(heartPack.buffer));
-    //   // return pong;
-    // }
+    if (this.gameChanel.isOpen()) {
+      // console.log('this.gameChanel.isOpen()', this.gameChanel.isOpen());
+      this.sendWertcHeartPack(this.gameChanel);
+      // const heartPack = new DataView(new ArrayBuffer(4));
+      // heartPack.setUint32(0, 2009889916);
+      // this.gameChanel.sendMessageBinary(Buffer.from(heartPack.buffer));
+      // return pong;
+    }
     return pong;
   }
 
@@ -78,10 +84,10 @@ export class MetaGateway
     console.log('handleInitWebRtc');
 
     this.peer = new PeerConnection('roomTest', {
-      // iceServers: ['172.18.156.41:3478'],
-      portRangeBegin: 52000,
-      portRangeEnd: 53000,
-      iceServers: ['stun:120.24.252.95:3478', 'stun:172.18.156.41:3478'],
+      // iceServers: ['stun:stun.l.google.com:19302'],
+      // portRangeBegin: 52000,
+      // portRangeEnd: 53000,
+      iceServers: ['stun:120.24.252.95:3478'],
     });
 
     this.peer.onLocalDescription((sdp, type) => {
@@ -97,6 +103,19 @@ export class MetaGateway
     });
 
     this.peer.onLocalCandidate((candidate, mid) => {
+
+      if (/172\./.test(candidate)) {
+        console.error('private Ip');
+        if (candidate.includes(process.env.PRIVATE_IP)) {
+          console.error('PRIVATE_IP', process.env.PRIVATE_IP);
+          candidate = candidate.replace(
+            process.env.PRIVATE_IP,
+            process.env.PUBLIC_IP,
+          );
+        } else {
+          return;
+        }
+      }
       console.warn('onLocalCandidate Candidate:', candidate);
       const iceRes = {
         candidate,
@@ -126,6 +145,11 @@ export class MetaGateway
     this.peer.onGatheringStateChange((state) => {
       console.log('GatheringState:', state);
     });
+
+    this.peer.onTrack((track) => {
+      console.log('track', track);
+    });
+
     this.gameChanel = this.peer.createDataChannel('game-input');
 
     this.peer.onDataChannel((dc) => {
@@ -134,14 +158,15 @@ export class MetaGateway
 
     this.gameChanel.onOpen(() => {
       console.log('channel is open');
-      const peers = this.peer.getSelectedCandidatePair();
-      console.log('peers', peers);
+      debugger;
       clearInterval(this.timer);
+
       let i = 1;
       const paths = path.join(__dirname, '../ws/video/v11');
       console.error('__dirname', __dirname);
       console.error('paths', paths);
       if (this.gameChanel.isOpen()) {
+        console.log('gameChanel', this.gameChanel.isOpen());
         this.sendWertcHeartPack(this.gameChanel);
       }
       this.timer = setInterval(() => {
@@ -165,6 +190,7 @@ export class MetaGateway
     this.gameChanel.onClosed(() => {
       console.log('gameChanel close');
       this.stopSendWertcHeartPack();
+      cleanup();
     });
     this.gameChanel.onMessage((event) => {
       console.log('gameChanel onMessage', event);
@@ -180,7 +206,7 @@ export class MetaGateway
     heartPack.setUint32(0, 2009889916);
     this._webrtcInterval = setInterval(() => {
       if (channel.isOpen()) {
-        // console.log('gameChanel', channel);
+        console.log('gameChanel', channel);
         channel.sendMessageBinary(Buffer.from(heartPack.buffer));
       }
     }, 1e3);
@@ -194,7 +220,7 @@ export class MetaGateway
   handlerIceCandidate(client: any, payload: any) {
     const iceCandidate = Buffer.from(payload, 'base64').toString('utf-8');
     const candidate = JSON.parse(iceCandidate);
-    console.error('收到ice_candidate', candidate);
+    console.warn('收到ice_candidate', candidate);
     this.peer.addRemoteCandidate(candidate.candidate, candidate.sdpMid);
   }