perfectnogation #20
			
				
			
		
		
		
	| 
						 | 
				
			
			@ -4,8 +4,7 @@ export interface IConnection{
 | 
			
		|||
    endpoint: string;
 | 
			
		||||
    autoReconnect?: boolean | {
 | 
			
		||||
        timeout: number;
 | 
			
		||||
    },
 | 
			
		||||
    autoCreateRTC: boolean
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
export class Connection
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -36,9 +35,6 @@ export class Connection
 | 
			
		|||
                throw new Error("endpoint is required")
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if(options.autoCreateRTC === false){
 | 
			
		||||
            mwse.autoCreateRTC = false;
 | 
			
		||||
        }
 | 
			
		||||
        if(typeof options.autoReconnect == "boolean")
 | 
			
		||||
        {
 | 
			
		||||
            this.autoReconnect = true;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,26 +22,18 @@ export default class Peer extends EventTarget
 | 
			
		|||
    public selfSocket : boolean = false;
 | 
			
		||||
    public active : boolean = false;
 | 
			
		||||
    public info : PeerInfo;
 | 
			
		||||
    public rtc? : WebRTC;
 | 
			
		||||
    public rtc : WebRTC;
 | 
			
		||||
    public peerConnection : boolean = false;
 | 
			
		||||
    public primaryChannel : "websocket" | "datachannel" = "datachannel";
 | 
			
		||||
    constructor(wsts:MWSE){
 | 
			
		||||
        super();
 | 
			
		||||
        this.mwse = wsts;
 | 
			
		||||
        this.rtc = this.createRTC();
 | 
			
		||||
        this.info = new PeerInfo(this);
 | 
			
		||||
        this.on('pack',(data:{type?:string,action?:IMessageSymbase,payload?:any}) => {
 | 
			
		||||
            if(data.type == ':rtcpack:')
 | 
			
		||||
            {
 | 
			
		||||
                if(this.rtc)
 | 
			
		||||
                {
 | 
			
		||||
                    return this.rtc.emit("input", data.payload)
 | 
			
		||||
                }else{
 | 
			
		||||
                    if(this.mwse.autoCreateRTC)
 | 
			
		||||
                    {
 | 
			
		||||
                        this.createRTC().emit("input", data.payload);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                return console.warn("Not active rtc but recaived rtc packs")
 | 
			
		||||
                return this.rtc.emit("input", data.payload)
 | 
			
		||||
            };
 | 
			
		||||
            this.emit("message", data);
 | 
			
		||||
        });
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -59,6 +59,8 @@ export default class WebRTC
 | 
			
		|||
    public ignoreOffer = false;
 | 
			
		||||
    public isSettingRemoteAnswerPending = false;
 | 
			
		||||
 | 
			
		||||
    candicatePack : RTCIceCandidate[] = [];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    constructor(
 | 
			
		||||
        rtcConfig?: RTCConfiguration,
 | 
			
		||||
| 
						 | 
				
			
			@ -112,7 +114,17 @@ export default class WebRTC
 | 
			
		|||
            switch(data.type)
 | 
			
		||||
            {
 | 
			
		||||
                case "icecandidate":{
 | 
			
		||||
                    await this.rtc.addIceCandidate(new RTCIceCandidate(data.value));
 | 
			
		||||
                    try{
 | 
			
		||||
                        if(this.rtc.remoteDescription){
 | 
			
		||||
                            await this.rtc.addIceCandidate(new RTCIceCandidate(data.value));
 | 
			
		||||
                        }else{
 | 
			
		||||
                            this.candicatePack.push(new RTCIceCandidate(data.value))
 | 
			
		||||
                        }
 | 
			
		||||
                    }catch(error){
 | 
			
		||||
                        debugger;
 | 
			
		||||
                    }finally{
 | 
			
		||||
                        console.log("ICE Canbet")
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                case "offer":{
 | 
			
		||||
| 
						 | 
				
			
			@ -132,6 +144,9 @@ export default class WebRTC
 | 
			
		|||
 | 
			
		||||
                    this.isSettingRemoteAnswerPending = false;
 | 
			
		||||
 | 
			
		||||
                    for (const candidate of this.candicatePack) {
 | 
			
		||||
                        await this.rtc.addIceCandidate(candidate);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    let answer = await this.rtc.createAnswer({
 | 
			
		||||
                        offerToReceiveAudio: true,
 | 
			
		||||
| 
						 | 
				
			
			@ -145,19 +160,11 @@ export default class WebRTC
 | 
			
		|||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                case "answer":{
 | 
			
		||||
                    
 | 
			
		||||
                    let readyForOffer = !this.makingOffer && (this.rtc.signalingState == "stable" || this.isSettingRemoteAnswerPending);
 | 
			
		||||
 | 
			
		||||
                    const offerCollision = !readyForOffer;
 | 
			
		||||
 | 
			
		||||
                    this.ignoreOffer = !this.isPolite() && offerCollision;
 | 
			
		||||
 | 
			
		||||
                    if(this.ignoreOffer){
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    this.isSettingRemoteAnswerPending = true;
 | 
			
		||||
                    await this.rtc.setRemoteDescription(new RTCSessionDescription(data.value))
 | 
			
		||||
                    this.isSettingRemoteAnswerPending = false;
 | 
			
		||||
 | 
			
		||||
                    for (const candidate of this.candicatePack) {
 | 
			
		||||
                        await this.rtc.addIceCandidate(candidate);
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                case "streamInfo":{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,7 +17,6 @@ export default class MWSE extends EventTarget {
 | 
			
		|||
    public peers : Map<string, Peer> = new Map();
 | 
			
		||||
    public virtualPressure : IPPressure;
 | 
			
		||||
    public me! : Peer;
 | 
			
		||||
    public autoCreateRTC : boolean = true;
 | 
			
		||||
    /*public static compress(message:string, callback:(e:any) => any)
 | 
			
		||||
    {
 | 
			
		||||
        let u : any= [];
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +34,7 @@ export default class MWSE extends EventTarget {
 | 
			
		|||
    }*/
 | 
			
		||||
    constructor(options: IConnection){
 | 
			
		||||
        super();
 | 
			
		||||
        MWSE.rtc = MWSE as unknown as WebRTC;
 | 
			
		||||
        this.server = new Connection(this,options);
 | 
			
		||||
        this.WSTSProtocol = new WSTSProtocol(this);
 | 
			
		||||
        this.EventPooling = new EventPool(this);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,14 +6,6 @@ let mwse;
 | 
			
		|||
 * @type {string}
 | 
			
		||||
 */
 | 
			
		||||
let mySocketId;
 | 
			
		||||
/**
 | 
			
		||||
 * @type {string}
 | 
			
		||||
 */
 | 
			
		||||
let myIPAddress;
 | 
			
		||||
/**
 | 
			
		||||
 * @type {string}
 | 
			
		||||
 */
 | 
			
		||||
let myNumber;
 | 
			
		||||
/**
 | 
			
		||||
 * @type {string}
 | 
			
		||||
 */
 | 
			
		||||
| 
						 | 
				
			
			@ -41,6 +33,14 @@ function connect()
 | 
			
		|||
    mwse = new MWSE({
 | 
			
		||||
        endpoint: "wss://ws.saqut.com"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    MWSE.rtc.defaultICEServers = [{
 | 
			
		||||
        urls: "turn:20.166.82.187:3478",
 | 
			
		||||
        username: "turnserver",
 | 
			
		||||
        credential: "turnserver"
 | 
			
		||||
    },{
 | 
			
		||||
        urls: "stun:stun.l.google.com:19302"
 | 
			
		||||
    }];
 | 
			
		||||
    
 | 
			
		||||
    mwse.scope(beginEngine);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -124,8 +124,6 @@ async function beginEngine()
 | 
			
		|||
    let me = mwse.peer("me");
 | 
			
		||||
    me.disablePairAuth();
 | 
			
		||||
    mySocketId = me.socketId;
 | 
			
		||||
    myIPAddress = await mwse.virtualPressure.allocAPIPAddress();
 | 
			
		||||
    myNumber = await mwse.virtualPressure.allocAPNumber();
 | 
			
		||||
 | 
			
		||||
    let url = new URL(window.location);
 | 
			
		||||
    roomid = url.searchParams.get("room");
 | 
			
		||||
| 
						 | 
				
			
			@ -203,11 +201,13 @@ async function connectRoom()
 | 
			
		|||
    });
 | 
			
		||||
    await room.createRoom();
 | 
			
		||||
    
 | 
			
		||||
    room.on("join", peer => IncomingPeer(peer, true));
 | 
			
		||||
    room.on("join", peer => IncomingPeer(peer,true));
 | 
			
		||||
    room.on("eject", peer => OutgoingPeer(peer));
 | 
			
		||||
 | 
			
		||||
    for (const peer of await room.fetchPeers()) {
 | 
			
		||||
        IncomingPeer(peer)
 | 
			
		||||
        if(peer.socketId != mwse.peer('me').socketId){
 | 
			
		||||
            IncomingPeer(peer,false)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    addVideoList("My Webcam",outgoingStream, mwse.peer("me"), true)
 | 
			
		||||
| 
						 | 
				
			
			@ -219,21 +219,13 @@ async function connectRoom()
 | 
			
		|||
 */
 | 
			
		||||
function IncomingPeer(peer,activeConnect)
 | 
			
		||||
{
 | 
			
		||||
    peer.createRTC({
 | 
			
		||||
        iceCandidatePoolSize: 0
 | 
			
		||||
    },[{
 | 
			
		||||
        urls: "turn:20.166.82.187:3478",
 | 
			
		||||
        username: "turnserver",
 | 
			
		||||
        credential: "turnserver"
 | 
			
		||||
    },{
 | 
			
		||||
        urls: "stun:stun.l.google.com:19302"
 | 
			
		||||
    }]);
 | 
			
		||||
    if(activeConnect)
 | 
			
		||||
    {
 | 
			
		||||
        peer.rtc.connect();
 | 
			
		||||
    }
 | 
			
		||||
    peer.rtc.rtc.turboBitrate = 0;
 | 
			
		||||
    peer.rtc.on('connected',() => {
 | 
			
		||||
        console.log("Connected");
 | 
			
		||||
        if(!activeConnect)
 | 
			
		||||
        {
 | 
			
		||||
            peer.rtc.sendStream(outgoingStream, "Webcam", {});
 | 
			
		||||
| 
						 | 
				
			
			@ -241,6 +233,7 @@ function IncomingPeer(peer,activeConnect)
 | 
			
		|||
        }
 | 
			
		||||
    });
 | 
			
		||||
    peer.rtc.on('disconnected',() => {
 | 
			
		||||
        console.log("Disconnected");
 | 
			
		||||
        removeVideoList(peer.streamY, peer);
 | 
			
		||||
        delete activePeers[peer.socketId];
 | 
			
		||||
    });
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										4296
									
								
								script/index.js
								
								
								
								
							
							
						
						
									
										4296
									
								
								script/index.js
								
								
								
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Loading…
	
		Reference in New Issue