|
@@ -22,6 +22,7 @@ import { Logger } from '@nestjs/common';
|
|
|
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');
|
|
|
|
|
@@ -34,7 +35,10 @@ initLogger('Debug');
|
|
|
export class MetaGateway
|
|
|
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
|
|
|
{
|
|
|
- constructor(private readonly sceneService: SceneService) {}
|
|
|
+ constructor(
|
|
|
+ private readonly sceneService: SceneService,
|
|
|
+ private readonly configService: ConfigService,
|
|
|
+ ) {}
|
|
|
private logger: Logger = new Logger('MetaGateway');
|
|
|
private peer: PeerConnection = null;
|
|
|
private timer: NodeJS.Timeout;
|
|
@@ -79,40 +83,44 @@ export class MetaGateway
|
|
|
|
|
|
@SubscribeMessage('init_webrtc')
|
|
|
handleInitWebRtc(client: any, payload: any): void {
|
|
|
- console.log('handleInitWebRtc');
|
|
|
- console.log('this.sceneService', this.sceneService);
|
|
|
- const stun_server: string[] = Array.from(
|
|
|
- String(process.env.STUNS_SEVER).split(','),
|
|
|
- );
|
|
|
- this.logger.log('stun_server', stun_server);
|
|
|
+ this.logger.log('action::handleInitWebRtc', JSON.stringify(payload));
|
|
|
+
|
|
|
+ const stun_server = this.configService.get('stun.server');
|
|
|
+ const portRangeBegin = this.configService.get('stun.portRangeBegin');
|
|
|
+ const portRangeEnd = this.configService.get('stun.portRangeEnd');
|
|
|
+
|
|
|
this.peer = new PeerConnection('roomTest', {
|
|
|
- portRangeBegin: 52000,
|
|
|
- portRangeEnd: 53000,
|
|
|
+ portRangeBegin: portRangeBegin,
|
|
|
+ portRangeEnd: portRangeEnd,
|
|
|
iceServers: stun_server,
|
|
|
});
|
|
|
|
|
|
this.peer.onLocalDescription((sdp, type) => {
|
|
|
- console.warn('peer SDP:', sdp, ' Type:', type);
|
|
|
+ // console.warn('peer SDP:', sdp, ' Type:', type);
|
|
|
const offer = { sdp, type };
|
|
|
const offerFormat = {
|
|
|
id: 'offer',
|
|
|
data: Buffer.from(JSON.stringify(offer)).toString('base64'),
|
|
|
};
|
|
|
- console.log('send', offerFormat);
|
|
|
+ this.logger.log('peer::onLocalDescription', JSON.stringify(offerFormat));
|
|
|
client.send(JSON.stringify(offerFormat));
|
|
|
});
|
|
|
|
|
|
const replaceToPublic = (candidate) => {
|
|
|
- console.warn('PRIVATE_IP', process.env.PRIVATE_IP);
|
|
|
- return candidate.replace(process.env.PRIVATE_IP, process.env.PUBLIC_IP);
|
|
|
+ const PRIVATE_IP = this.configService.get('server.private_ip');
|
|
|
+ const PUBLIC_IP = this.configService.get('server.public_ip');
|
|
|
+ this.logger.log(
|
|
|
+ 'peer::replaceToPublic',
|
|
|
+ `private_ip:${PRIVATE_IP} to public_ip:${PUBLIC_IP}`,
|
|
|
+ );
|
|
|
+ return candidate.replace(PRIVATE_IP, PUBLIC_IP);
|
|
|
};
|
|
|
|
|
|
this.peer.onLocalCandidate((candidate, mid) => {
|
|
|
if (/172\./.test(candidate)) {
|
|
|
console.error('private Ip process', candidate);
|
|
|
-
|
|
|
- if (candidate.includes(process.env.PRIVATE_IP)) {
|
|
|
- console.error('PRIVATE_IP', process.env.PRIVATE_IP);
|
|
|
+ const PRIVATE_IP = this.configService.get('server.private_ip');
|
|
|
+ if (candidate.includes(PRIVATE_IP)) {
|
|
|
candidate = replaceToPublic(candidate);
|
|
|
} else {
|
|
|
return;
|