|
@@ -18,13 +18,14 @@ import {
|
|
|
} from 'node-datachannel';
|
|
|
|
|
|
import { Buffer } from 'buffer';
|
|
|
-import { Logger } from '@nestjs/common';
|
|
|
+import { Inject } from '@nestjs/common';
|
|
|
import * as path from 'path';
|
|
|
import { createReadStream } from 'fs';
|
|
|
import { SceneService } from './scene/scene.service';
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
import { stringify } from 'querystring';
|
|
|
-
|
|
|
+import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
|
|
|
+import { Logger } from 'winston';
|
|
|
// 'Verbose' | 'Debug' | 'Info' | 'Warning' | 'Error' | 'Fatal';
|
|
|
initLogger('Debug');
|
|
|
|
|
@@ -35,13 +36,13 @@ initLogger('Debug');
|
|
|
path: '/ws',
|
|
|
})
|
|
|
export class MetaGateway
|
|
|
- implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
|
|
|
-{
|
|
|
+ implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
|
|
|
constructor(
|
|
|
private readonly sceneService: SceneService,
|
|
|
private readonly configService: ConfigService,
|
|
|
- ) {}
|
|
|
- private logger: Logger = new Logger('MetaGateway');
|
|
|
+ @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
|
|
|
+ ) { }
|
|
|
+ // private logger: Logger = new Logger('MetaGateway');
|
|
|
private peer: PeerConnection = null;
|
|
|
private timer: NodeJS.Timeout;
|
|
|
private _webrtcInterval: NodeJS.Timeout;
|
|
@@ -52,17 +53,13 @@ export class MetaGateway
|
|
|
private startstreamingSub: any;
|
|
|
@WebSocketServer() server: Server;
|
|
|
|
|
|
- // @SubscribeMessage('message')
|
|
|
- // handleMessage(client: any, payload: any) {
|
|
|
- // this.logger.log(`payload: ${JSON.stringify(payload)}`);
|
|
|
- // }
|
|
|
afterInit(server: Server) {
|
|
|
- this.logger.log('Init MetaGateway');
|
|
|
+ this.logger.info('Init MetaGateway');
|
|
|
}
|
|
|
|
|
|
@SubscribeMessage('init')
|
|
|
handleInit(client: any, payload: any) {
|
|
|
- this.logger.log(`socket::init: ${JSON.stringify(payload)}`);
|
|
|
+ this.logger.info(`socket::init: ${JSON.stringify(payload)}`);
|
|
|
}
|
|
|
|
|
|
@SubscribeMessage('heartbeat')
|
|
@@ -90,7 +87,8 @@ export class MetaGateway
|
|
|
handleInitWebRtc(client: any, payload: any): void {
|
|
|
// TODO 可能会中断连接
|
|
|
// cleanup();
|
|
|
- this.logger.log('action::handleInitWebRtc', JSON.stringify(payload));
|
|
|
+ this.logger.info('action::handleInitWebRtc', JSON.stringify(payload));
|
|
|
+ this.logger.log('http', `socket::开始`);
|
|
|
const stun_server = this.configService.get('stun.server');
|
|
|
const portRangeBegin = this.configService.get('stun.portRangeBegin');
|
|
|
const portRangeEnd = this.configService.get('stun.portRangeEnd');
|
|
@@ -112,14 +110,14 @@ export class MetaGateway
|
|
|
id: 'offer',
|
|
|
data: Buffer.from(JSON.stringify(offer)).toString('base64'),
|
|
|
};
|
|
|
- this.logger.log('peer::onLocalDescription', JSON.stringify(offerFormat));
|
|
|
+ this.logger.info('peer::onLocalDescription', JSON.stringify(offerFormat));
|
|
|
client.send(JSON.stringify(offerFormat));
|
|
|
});
|
|
|
|
|
|
const replaceToPublic = (candidate) => {
|
|
|
const PRIVATE_IP = this.configService.get('server.private_ip');
|
|
|
const PUBLIC_IP = this.configService.get('server.public_ip');
|
|
|
- this.logger.log(
|
|
|
+ this.logger.info(
|
|
|
'peer::replaceToPublic',
|
|
|
`private_ip:${PRIVATE_IP} to public_ip:${PUBLIC_IP}`,
|
|
|
);
|
|
@@ -128,7 +126,9 @@ export class MetaGateway
|
|
|
|
|
|
this.peer.onLocalCandidate((candidate, mid) => {
|
|
|
if (/172\./.test(candidate)) {
|
|
|
- this.logger.log('server private Ip process', JSON.stringify(candidate));
|
|
|
+ this.logger.info(
|
|
|
+ 'server private Ip process' + JSON.stringify(candidate),
|
|
|
+ );
|
|
|
const PRIVATE_IP = this.configService.get('server.private_ip');
|
|
|
if (candidate.includes(PRIVATE_IP)) {
|
|
|
candidate = replaceToPublic(candidate);
|
|
@@ -142,10 +142,6 @@ export class MetaGateway
|
|
|
this.logger.warn('不是192.168.0./192.168.10测试网段' + candidate);
|
|
|
return;
|
|
|
}
|
|
|
- // if (candidate.includes(process.env.PRIVATE_IP)) {
|
|
|
- // console.error('PRIVATE_IP', process.env.PRIVATE_IP);
|
|
|
- // candidate = replaceToPublic(candidate);
|
|
|
- // }
|
|
|
}
|
|
|
this.logger.warn('onLocalCandidate last Candidate:' + candidate);
|
|
|
|
|
@@ -192,15 +188,14 @@ export class MetaGateway
|
|
|
});
|
|
|
|
|
|
this.gameChanel.onOpen(() => {
|
|
|
- this.logger.log('channel is open');
|
|
|
if (this.gameChanel.isOpen()) {
|
|
|
+ this.logger.info('channel is open');
|
|
|
const peers = this.peer.getSelectedCandidatePair();
|
|
|
- this.logger.log('配对成功', JSON.stringify(peers));
|
|
|
- console.log('gameChanel', this.gameChanel.isOpen());
|
|
|
+ this.logger.info('配对成功::' + JSON.stringify(peers));
|
|
|
this.sendWertcHeartPack(this.gameChanel);
|
|
|
this.sceneService.handleDataChanelOpen(this.gameChanel, this.peer);
|
|
|
} else {
|
|
|
- console.log('gameChanel has problem');
|
|
|
+ this.logger.error('gameChanel has problem');
|
|
|
}
|
|
|
|
|
|
// Number.prototype.padLeft = function (n, str) {
|
|
@@ -243,8 +238,7 @@ export class MetaGateway
|
|
|
handlerIceCandidate(client: any, payload: any) {
|
|
|
const iceCandidate = Buffer.from(payload, 'base64').toString('utf-8');
|
|
|
const candidate = JSON.parse(iceCandidate);
|
|
|
- // console.warn('收到ice_candidate',);
|
|
|
- this.logger.log('server get ice_candidate', JSON.stringify(candidate));
|
|
|
+ this.logger.info('server get ice_candidate' + JSON.stringify(candidate));
|
|
|
this.peer.addRemoteCandidate(candidate.candidate, candidate.sdpMid);
|
|
|
}
|
|
|
|
|
@@ -292,7 +286,7 @@ export class MetaGateway
|
|
|
}
|
|
|
},
|
|
|
);
|
|
|
- } catch (error) {}
|
|
|
+ } catch (error) { }
|
|
|
}
|
|
|
|
|
|
handleConnection(client: WebSocket, ...args: any[]) {
|
|
@@ -310,7 +304,7 @@ export class MetaGateway
|
|
|
const reconnect = params.get('reconnect');
|
|
|
this.sceneService.setConfig(this.user_id, this.roomId);
|
|
|
|
|
|
- this.logger.log(`Client connected:`);
|
|
|
+ this.logger.info(`Client connected:`);
|
|
|
|
|
|
const connected = {
|
|
|
channel_id: '',
|
|
@@ -329,7 +323,7 @@ export class MetaGateway
|
|
|
client.send(tt);
|
|
|
}
|
|
|
handleDisconnect(client: WebSocket) {
|
|
|
- this.logger.log(`Client disconnected: ${client.id}`);
|
|
|
+ this.logger.info(`Client disconnected: ${client.id}`);
|
|
|
this.peer && this.peer.close();
|
|
|
if (this.startstreamingSub) {
|
|
|
this.startstreamingSub.unsubscribe();
|