From a5bd0e9443c4270f0d9ea02291994ab01c26a7eb Mon Sep 17 00:00:00 2001 From: Abdussamed Date: Thu, 27 Apr 2023 22:45:36 +0300 Subject: [PATCH] WebRTC Turn/Stun config --- frontend/WebRTC.ts | 63 +++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/frontend/WebRTC.ts b/frontend/WebRTC.ts index f4f39ca..8b4ea33 100644 --- a/frontend/WebRTC.ts +++ b/frontend/WebRTC.ts @@ -25,26 +25,53 @@ export default class WebRTC public events : { [eventname:string]: Function[] } = {}; public channel : RTCDataChannel | undefined; + public static defaultRTCConfig : RTCConfiguration = { + iceCandidatePoolSize: 0, + iceTransportPolicy:"all", + rtcpMuxPolicy:"require", + }; + + + public static defaultICEServers : RTCIceServer[] = [{ + urls: "stun:stun.l.google.com:19302" + },{ + urls: "stun:stun1.l.google.com:19302" + },{ + urls: "stun:stun2.l.google.com:19302" + },{ + urls: "stun:stun3.l.google.com:19302" + },{ + urls: "stun:stun4.l.google.com:19302" + }]; + public peer? : Peer; - constructor() + public FileTransportChannel? : P2PFileSender; + + constructor( + rtcConfig?: RTCConfiguration, + rtcServers?: RTCIceServer[] + ) { - this.rtc = new RTCPeerConnection({ - iceCandidatePoolSize: 0, - iceTransportPolicy:"all", - rtcpMuxPolicy:"require", - iceServers:[{ - urls: "stun:stun.l.google.com:19302" - },{ - urls: "stun:stun1.l.google.com:19302" - },{ - urls: "stun:stun2.l.google.com:19302" - },{ - urls: "stun:stun3.l.google.com:19302" - },{ - urls: "stun:stun4.l.google.com:19302" - }] - }); + let config : any = {}; + + if(rtcConfig) + { + Object.assign( + config, + WebRTC.defaultRTCConfig, + rtcConfig + ) + }else{ + Object.assign( + config, + WebRTC.defaultRTCConfig + ) + } + + config.iceServers = rtcServers || WebRTC.defaultICEServers; + + this.rtc = new RTCPeerConnection(config as RTCConfiguration); this.rtc.addEventListener("connectionstatechange",()=>{ this.eventConnectionState(); }) @@ -197,7 +224,7 @@ export default class WebRTC if(this.rtc) { this.rtc.close(); - // this.rtc = undefined; + //this.rtc = undefined; }; this.emit('disconnected'); WebRTC.channels.delete(this.id);