238 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			238 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import {Connection,IConnection} from "./Connection";
 | 
						|
import EventPool from "./EventPool";
 | 
						|
import EventTarget from "./EventTarget";
 | 
						|
import { IPPressure } from "./IPPressure";
 | 
						|
import Peer from "./Peer";
 | 
						|
import Room, { IRoomOptions } from "./Room";
 | 
						|
import WSTSProtocol, { Message } from "./WSTSProtocol";
 | 
						|
import WebRTC from "./WebRTC";
 | 
						|
//import {Gzip} from "fflate";
 | 
						|
export default class MWSE extends EventTarget {
 | 
						|
    public static rtc : WebRTC;
 | 
						|
    public server! : Connection;
 | 
						|
    public WSTSProtocol! : WSTSProtocol;
 | 
						|
    public EventPooling! : EventPool;
 | 
						|
    public rooms : Map<string, Room> = new Map();
 | 
						|
    public pairs : Map<string, Peer> = new Map();
 | 
						|
    public peers : Map<string, Peer> = new Map();
 | 
						|
    public virtualPressure : IPPressure;
 | 
						|
    public me! : Peer;
 | 
						|
    /*public static compress(message:string, callback:(e:any) => any)
 | 
						|
    {
 | 
						|
        let u : any= [];
 | 
						|
        let C = new Gzip({
 | 
						|
            level: 9,
 | 
						|
            mem: 12
 | 
						|
        },(stream,isLast) => {
 | 
						|
            u.push(stream);
 | 
						|
            if(isLast)
 | 
						|
            {
 | 
						|
                callback(u);
 | 
						|
            }
 | 
						|
        });
 | 
						|
        C.push(new TextEncoder().encode(message), true);
 | 
						|
    }*/
 | 
						|
    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);
 | 
						|
        this.virtualPressure = new IPPressure(this);
 | 
						|
        this.server.connect();
 | 
						|
        this.me = new Peer(this);
 | 
						|
        this.me.scope(()=>{
 | 
						|
            this.peers.set('me', this.me);
 | 
						|
            this.peers.set(this.me.socketId as string, this.me);
 | 
						|
        })
 | 
						|
        this.server.onActive(async ()=>{
 | 
						|
            this.me.setSocketId('me');
 | 
						|
            await this.me.metadata();
 | 
						|
            this.emit('scope');
 | 
						|
            this.activeScope = true;
 | 
						|
        });
 | 
						|
        this.server.onPassive(async ()=>{
 | 
						|
            this.emit('close');
 | 
						|
        });
 | 
						|
        this.packMessagingSystem();
 | 
						|
    }
 | 
						|
 | 
						|
    public writable = 1;
 | 
						|
    public readable = 1;
 | 
						|
 | 
						|
    public destroy()
 | 
						|
    {
 | 
						|
        this.server.disconnect();
 | 
						|
    }
 | 
						|
 | 
						|
    public enableRecaiveData(){
 | 
						|
        this.WSTSProtocol.SendOnly({ type: 'connection/packrecaive', value: 1 })
 | 
						|
        this.readable = 1
 | 
						|
    }
 | 
						|
    public disableRecaiveData(){
 | 
						|
        this.WSTSProtocol.SendOnly({ type: 'connection/packrecaive', value: 0 })
 | 
						|
        this.readable = 0
 | 
						|
    }
 | 
						|
 | 
						|
    public enableSendData(){
 | 
						|
        this.WSTSProtocol.SendOnly({ type: 'connection/packsending', value: 1 })
 | 
						|
        this.writable = 1
 | 
						|
    }
 | 
						|
    public disableSendData(){
 | 
						|
        this.WSTSProtocol.SendOnly({ type: 'connection/packsending', value: 0 })
 | 
						|
        this.writable = 0
 | 
						|
    }
 | 
						|
 | 
						|
    public enableNotifyRoomInfo(){
 | 
						|
        this.WSTSProtocol.SendOnly({ type: 'connection/roominfo', value: 1 })
 | 
						|
    }
 | 
						|
    public disableNotifyRoomInfo(){
 | 
						|
        this.WSTSProtocol.SendOnly({ type: 'connection/roominfo', value: 0 })
 | 
						|
    }
 | 
						|
 | 
						|
    public async request(peerId: string, pack:Message)
 | 
						|
    {
 | 
						|
        let {pack:answer} = await this.EventPooling.request({
 | 
						|
            type: 'request/to',
 | 
						|
            to: peerId,
 | 
						|
            pack
 | 
						|
        });
 | 
						|
        return answer;
 | 
						|
    }
 | 
						|
    public async response(peerId: string, requestId:number, pack:Message)
 | 
						|
    {
 | 
						|
        this.WSTSProtocol.SendOnly({
 | 
						|
            type: 'response/to',
 | 
						|
            to: peerId,
 | 
						|
            pack,
 | 
						|
            id: requestId
 | 
						|
        })
 | 
						|
    }
 | 
						|
    private packMessagingSystem()
 | 
						|
    {
 | 
						|
        this.EventPooling.signal('pack',(payload : {from:string,pack:any}) => {
 | 
						|
            if(this.readable)
 | 
						|
            {
 | 
						|
                let {from,pack} = payload;
 | 
						|
                this.peer(from, true).emit('pack', pack);
 | 
						|
            }
 | 
						|
        })
 | 
						|
        this.EventPooling.signal('request',(payload : {from:string,pack:any,id:number}) => {
 | 
						|
            let {from,pack, id} = payload;
 | 
						|
            let scope = {
 | 
						|
                body: pack,
 | 
						|
                response: (pack: Message) => {
 | 
						|
                    this.response(from, id, pack);
 | 
						|
                },
 | 
						|
                peer: this.peer(from, true)
 | 
						|
            };
 | 
						|
            this.peer(from, true).emit('request', scope);
 | 
						|
            this.peer('me').emit('request', scope);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal('pack/room',(payload : {from:string,pack:any,sender:string}) => {
 | 
						|
            if(this.readable)
 | 
						|
            {
 | 
						|
                let {from,pack,sender} = payload;
 | 
						|
                this.room(from).emit('message', pack, this.peer(sender));
 | 
						|
            }
 | 
						|
        })
 | 
						|
        this.EventPooling.signal('room/joined',(payload : {id:string,roomid:any,ownerid:string}) => {
 | 
						|
            let {id,roomid} = payload;
 | 
						|
            let room = this.room(roomid);
 | 
						|
            let peer = this.peer(id, true);
 | 
						|
            room.peers.set(peer.socketId as string, peer);
 | 
						|
            room.emit('join', peer);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal('room/info',(payload : {roomId:string,value:any,name:string}) => {
 | 
						|
            let {roomId,name,value} = payload;
 | 
						|
            this.room(roomId).emit('updateinfo', name,value);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal('room/ejected',(payload : {id:string,roomid:any,ownerid:string}) => {
 | 
						|
            let {id,roomid} = payload;
 | 
						|
            let room = this.room(roomid);
 | 
						|
            let peer = this.peer(id, true);
 | 
						|
            room.peers.delete(peer.socketId as string);
 | 
						|
            room.emit('eject', peer);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal('room/closed',(payload : {roomid:any}) => {
 | 
						|
            let {roomid} = payload;
 | 
						|
            let room = this.room(roomid);
 | 
						|
            room.peers.clear();
 | 
						|
            room.emit('close');
 | 
						|
            this.rooms.delete(roomid);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal("pair/info", (payload : {from : string,name: string, value: string | number | boolean}) => {
 | 
						|
            let {from, name, value} = payload;
 | 
						|
            let peer = this.peer(from, true);
 | 
						|
            peer.info.info[name] = value;
 | 
						|
            peer.emit("info", name, value);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal("request/pair", (payload : {from : string,info: any}) => {
 | 
						|
            let {from, info} = payload;
 | 
						|
            let peer = this.peer(from, true);
 | 
						|
            peer.info.info = info;
 | 
						|
            peer.emit("request/pair", peer);
 | 
						|
            this.peer('me').emit('request/pair', peer);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal("peer/disconnect", (payload : {id : string}) => {
 | 
						|
            let {id} = payload;
 | 
						|
            let peer = this.peer(id, true);
 | 
						|
            peer.emit("disconnect", peer);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal("accepted/pair", (payload : {from : string,info: any}) => {
 | 
						|
            let {from, info} = payload;
 | 
						|
            let peer = this.peer(from, true);
 | 
						|
            peer.info.info = info;
 | 
						|
            peer.emit("accepted/pair", peer);
 | 
						|
            this.peer('me').emit('accepted/pairr', peer);
 | 
						|
        })
 | 
						|
        this.EventPooling.signal("end/pair", (payload : {from : string,info: any}) => {
 | 
						|
            let {from, info} = payload;
 | 
						|
            let peer = this.peer(from, true);
 | 
						|
            peer.emit("endPair", info);
 | 
						|
            this.peer('me').emit('endPair', from, info);
 | 
						|
        })
 | 
						|
    }
 | 
						|
    public room(options: IRoomOptions | string) : Room
 | 
						|
    {
 | 
						|
        if(typeof options == "string")
 | 
						|
        {
 | 
						|
            if(this.rooms.has(options))
 | 
						|
            {
 | 
						|
                return this.rooms.get(options) as Room
 | 
						|
            }
 | 
						|
        }
 | 
						|
        let room = new Room(this);
 | 
						|
        room.setRoomOptions(options);
 | 
						|
        this.emit('room');
 | 
						|
        return room;
 | 
						|
    }
 | 
						|
    public peer(options: string | IRoomOptions, isActive = false) : Peer
 | 
						|
    {
 | 
						|
        if(typeof options == "string")
 | 
						|
        {
 | 
						|
            if(this.peers.has(options))
 | 
						|
            {
 | 
						|
                return this.peers.get(options) as Peer
 | 
						|
            }
 | 
						|
            if(this.pairs.has(options))
 | 
						|
            {
 | 
						|
                return this.pairs.get(options) as Peer
 | 
						|
            }
 | 
						|
        }
 | 
						|
        let peer = new Peer(this);
 | 
						|
        peer.setPeerOptions(options);
 | 
						|
        peer.active = isActive;
 | 
						|
        this.peers.set(peer.socketId as string, peer);
 | 
						|
        this.emit('peer', peer);
 | 
						|
        return peer;
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
declare global {
 | 
						|
    interface Window {
 | 
						|
        MWSE: any;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
window.MWSE = MWSE; |