133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
import {Connection,IConnection} from "./Connection";
|
|
import EventPool from "./EventPool";
|
|
import EventTarget from "./EventTarget";
|
|
import Peer from "./Peer";
|
|
import Room, { IRoomOptions } from "./Room";
|
|
import WSTSProtocol, { Message } from "./WSTSProtocol";
|
|
export default class MWSE extends EventTarget {
|
|
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 me! : Peer;
|
|
constructor(options: IConnection){
|
|
super();
|
|
this.server = new Connection(options);
|
|
this.server.connect();
|
|
this.WSTSProtocol = new WSTSProtocol(this);
|
|
this.EventPooling = new EventPool(this);
|
|
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.packMessagingSystem();
|
|
}
|
|
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 : {to:string,pack:any}) => {
|
|
let {to,pack} = payload;
|
|
this.peer(to, true).emit('message', pack);
|
|
})
|
|
this.EventPooling.signal('request',(payload : {from:string,pack:any,id:number}) => {
|
|
let {from,pack, id} = payload;
|
|
this.peer(from, true).emit('request', {
|
|
body: pack,
|
|
response: (pack: Message) => {
|
|
this.response(from, id, pack);
|
|
}
|
|
});
|
|
})
|
|
this.EventPooling.signal('pack/room',(payload : {to:string,pack:any}) => {
|
|
let {to,pack} = payload;
|
|
this.room(to).emit('message', pack);
|
|
})
|
|
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.emit('join', peer);
|
|
})
|
|
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);
|
|
room.emit('eject', peer);
|
|
})
|
|
this.EventPooling.signal('room/closed',(payload : {roomid:any}) => {
|
|
let {roomid} = payload;
|
|
let room = this.room(roomid);
|
|
room.emit('close');
|
|
this.rooms.delete(roomid);
|
|
})
|
|
}
|
|
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.rooms.set(room.roomId as string, room);
|
|
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; |